這一篇我們來聊聊私有倉庫的搭建,其實不管你是通過docker build還是compose的方式進行構建的映象,最終還是要將生成好的映象push到遠端的倉庫中,這樣
多個平臺可以方便的獲取你registry中的映象,否則你不還得帶著你的原始碼到處跑不是? 而且私有倉庫還利於映象的獲取分發,反正都是內網,映象構建的再大又能怎樣?
一:registry server映象倉庫
你可以到dockerhub上去找一個叫registry server的映象,如下圖: https://hub.docker.com/_/registry
這裡我有兩臺機器:
registry: 192.168.23.147
client: 192.168.23.146
接下來根據registry的文件描述,我在147機器上執行以下docker run 命令,開放5000埠。
[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。
[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的又何妨呢,本篇就先說到這裡,希望對你有幫助。