8天入門docker系列 —— 第六天 搭建本身的私有鏡像倉庫Registry

  

         這一篇咱們來聊聊私有倉庫的搭建,其實無論你是經過docker build仍是compose的方式進行構建的鏡像,最終仍是要將生成好的鏡像push到遠程的倉庫中,這樣nginx

多個平臺能夠方便的獲取你registry中的鏡像,不然你不還得帶着你的源代碼處處跑不是? 並且私有倉庫還利於鏡像的獲取分發,反正都是內網,鏡像構建的再大又能怎樣?docker

 

一:registry server鏡像倉庫json

       

             你能夠到dockerhub上去找一個叫registry server的鏡像,以下圖:  https://hub.docker.com/_/registryvim

 

這裏我有兩臺機器: 跨域

registry: 192.168.23.147安全

client: 192.168.23.146服務器

 

接下來根據registry的文檔描述,我在147機器上執行如下docker run 命令,開放5000端口。app

[root@localhost ~]# docker run -d -p 5000:5000 --restart always --name registry registry:2
Unable to find image 'registry:2' locally
2: Pulling from library/registry
c87736221ed0: Already exists 
1cc8e0bb44df: Already exists 
54d33bcb37f5: Already exists 
e8afc091c171: Already exists 
b4541f6d3db6: Already exists 
Digest: sha256:8004747f1e8cd820a148fb7499d71a76d45ff66bac6a29129bfdbfdc0154d146
Status: Downloaded newer image for registry:2
80199d4030ed0c444bd27f255201b01e2f5e89abfb4e5d2cd9c61cbbd428baaf
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
80199d4030ed        registry:2          "/entrypoint.sh /etc…"   15 seconds ago      Up 13 seconds       0.0.0.0:5000->5000/tcp   registry

 

      從上圖能夠看到 host機的5000端口已經開放,接下來我經過146機器從dockerhub上拉取一個nginx鏡像,而後打包成147前綴的倉庫地址,而後作push。tcp

[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
fc7181108d40: Pull complete 
d2e987ca2267: Pull complete 
0b760b431b11: Pull complete 
Digest: sha256:96fb261b66270b900ea5a2c17a26abbfabe95506e73c3a3c65869a6dbe83223a
Status: Downloaded newer image for nginx:latest
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              f68d6e55e065        2 days ago          109MB
[root@localhost ~]# docker tag nginx 192.168.23.147:5000/pnginx 
[root@localhost ~]# docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
192.168.23.147:5000/pnginx   latest              f68d6e55e065        2 days ago          109MB
nginx                        latest              f68d6e55e065        2 days ago          109MB
[root@localhost ~]# docker push 192.168.23.147:5000/pnginx
The push refers to repository [192.168.23.147:5000/pnginx]
Get https://192.168.23.147:5000/v2/: http: server gave HTTP response to HTTPS client

     

       臥槽,報錯了,從錯誤信息中能夠看到,https的client不接受http的response,沒辦法,你得要讓client端接收不安全的http應該就能夠了,這個問題在官方文工具

檔有專門的解決辦法,因此在daemon.json 加上一個不安全的http地址便可,以下圖: https://docs.docker.com/registry/insecure/

 

    

 

  有了這個解決方案,在/etc/docker/daemon.json 中將前面的域名改爲 192.168.23.147, 重啓docker,從下面output能夠看到推送成功。

[root@localhost ~]# vim /etc/docker/daemon.json
[root@localhost ~]# service docker restart
Redirecting to /bin/systemctl restart docker.service
[root@localhost ~]# docker push 192.168.23.147:5000/pnginx
The push refers to repository [192.168.23.147:5000/pnginx]
d2f0b6dea592: Layer already exists 
197c666de9dd: Layer already exists 
cf5b3c6798f7: Layer already exists 
latest: digest: sha256:00be67d6ba53d5318cd91c57771530f5251cfbe028b7be2c4b70526f988cfc9f size: 948
[root@localhost ~]# 

 

二:docker-registry-ui 可視化UI

     

         鏡像是推送上去了,但在147上沒法查看推送上來的鏡像信息,還得求助於開源社區,在dockerhub你能夠找到一款docker-registry-ui的registry的可視化UI的

工具,以下圖: https://hub.docker.com/r/joxit/docker-registry-ui

           在官方文檔中能夠找獲得一個很是簡單的docker執行命令,這裏就在147上執行吧。

[root@localhost ~]# docker run -d -p 80:80 joxit/docker-registry-ui
Unable to find image 'joxit/docker-registry-ui:latest' locally
latest: Pulling from joxit/docker-registry-ui
e7c96db7181b: Pull complete 
3fb6217217ef: Pull complete 
d5443b40bab6: Pull complete 
Digest: sha256:59401aa3c3e29b721163f49f81a9be3698d269bd983a5c44d422bb6da2d263a2
Status: Downloaded newer image for joxit/docker-registry-ui:latest
31806479eb0fdff245ba5f9476bf84d28413f18ec3a96770ebf4f903034461a9

     

          由於容器開放了80端口,因此你能夠直接訪問: http://192.168.23.147,而後添加上registry server的地址,以下圖。

 

          當添加完以後,你會發現有一個「跨域請求」的錯誤,這是由於默認的registry server不容許這麼作,因此你得讓registry服務器執行可跨域,其實在官方文檔中

也提到了這個問題,能夠在registry的config配置文件中進行修改。

 

     接下來我根據文檔定義了一個config.yml文件。

version: 0.1
log:
  fields:
    service: registry
storage:
  cache:
    blobdescriptor: inmemory
  filesystem:
    rootdirectory: /var/lib/registry
http:
  addr: :5000
  headers:
    X-Content-Type-Options: [nosniff]
 Access-Control-Allow-Origin: ['*'] Access-Control-Allow-Methods: ['*']
    Access-Control-Max-Age: [1728000]
health:
  storagedriver:
    enabled: true
    interval: 10s
    threshold: 3

 

       再把原來的register容器kill掉,將上面的config.yml經過文件掛載的方式送到容器裏。

[root@localhost app]# docker rm -f registry
registry
[root@localhost app]# docker run -d -p 5000:5000 --name registry -v /app/config.yml:/etc/docker/registry/config.yml registry:2
c8aa9493ec2fea662c161861f6a952be3c30465deef9219e58f263db37719113
[root@localhost app]# ls 
config.yml

 

      最後我在146上從新推送一下,固然你也能夠將147上的registry的目錄掛載到volume上或者host filesystem。

[root@localhost ~]# docker push 192.168.23.147:5000/pnginx
The push refers to repository [192.168.23.147:5000/pnginx]
d2f0b6dea592: Pushed 
197c666de9dd: Pushed 
cf5b3c6798f7: Pushed 
latest: digest: sha256:00be67d6ba53d5318cd91c57771530f5251cfbe028b7be2c4b70526f988cfc9f size: 948

 

終於能夠在ui上看到client推送過來的鏡像了,是否是很開心,因爲是內網,就算你的image有個1,2g的又何妨呢,本篇就先說到這裏,但願對你有幫助。

相關文章
相關標籤/搜索