(一)registry定義和功能
一、registry註冊中心是鏡像存儲的位置。
二、功能:集中的存儲、分發鏡像的服務,docker registry就是這樣的服務。
三、Docker Registry組成:由三個部分組成:index,registry,registry client。node
(二)、docker registry包括公共的 docker registry 和私有的docker registry nginx
2.1 Docker Registry 公開服務是開放給用戶使用、容許用戶管理鏡像的 Registry 服務。通常這類公開服務容許用戶免費上傳、下載公開的鏡像,並可能提供收費服務供用戶管理私有鏡像。 最經常使用的是官方的Docker Hub也是默認的Registry,並擁有高質量的官方鏡像。國內的是阿里雲docker
2.二、私有 Docker Registry。用戶還能夠在本地搭建私有 Docker Registry。私有倉庫優點: 一、節省帶寬。 二、更加安全。 三、內部鏡像統一管理。json
備註:拉取鏡像倉庫鏡像的格式: docker pull <registry>[:<port>]/[<namespace>/]<name>:<tag> registry:倉庫服務器地址:不指定默認是docker hub port:端口;默認是443,由於是https協議 namespace:名稱空間,指是哪一個用戶的倉庫,若是是頂層倉庫,可省 name:倉庫名 tag:標籤名;默認是latest版本
(三)、搭建私有倉庫
一、下載 ubuntu
[root@otrs004097 ~]# docker pull registry Using default tag: latest latest: Pulling from library/registry c87736221ed0: Pulling fs layer 1cc8e0bb44df: Pulling fs layer 54d33bcb37f5: Pulling fs layer e8afc091c171: Waiting b4541f6d3db6: Waiting latest: Pulling from library/registry c87736221ed0: Pull complete 1cc8e0bb44df: Pull complete 54d33bcb37f5: Pull complete e8afc091c171: Pull complete b4541f6d3db6: Pull complete Digest: sha256:8004747f1e8cd820a148fb7499d71a76d45ff66bac6a29129bfdbfdc0154d146 Status: Downloaded newer image for registry:latest docker.io/library/registry:latest
二、經過registry鏡像啓動一個容器 vim
[root@otrs004097 ~]# docker run -d -v /opt/docker-registry:/var/lib/registry -p 5000:5000 --name registry registry 6c01265a36274493fd362ab76819f262e344f8259b9a7b3ae056140ec11d1ec8 -itd:在容器中打開一個僞終端進行交互操做,並在後臺運行; -v:把宿主機的/data/registry目錄綁定 到 容器/var/lib/registry目錄(這個目錄是registry容器中存放鏡像文件的目錄),來實現數據的持久化; -p:映射端口;訪問宿主機的5000端口就訪問到registry容器的服務了; --restart=always:這是重啓的策略,假如這個容器異常退出會自動重啓容器; --name registry:建立容器命名爲registry,你能夠隨便命名; registry:latest:這個是剛纔pull下來的鏡像;
三、查看運行情況 瀏覽器
root@otrs004097 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6c01265a3627 registry "/entrypoint.sh /etc…" 4 seconds ago Up 2 seconds 0.0.0.0:5000->5000/tcp registry
四、在瀏覽器輸入改地址:http://192.168.4.97:5000/v2/,說明正常
{}安全
五、上傳出現報錯的時候 Get https://192.168.10.102:5000/v1/_ping: http: server gave HTTP response to HTTPS client
解決方法有兩種:
方法一:修改註冊中心文件/etc/docker/daemon.json,並重啓docker服務器
[root@otrs004097 ~]# vim /etc/docker/daemon.json { "registry-mirrors": ["http://hub-mirror.c.163.com"], "insecure-registries": ["192.168.4.97:5000"] } [root@otrs004097 ~]# systemctl restart docker 註釋: insecure-registries----->開放註冊https協議 registry-mirrors----->倉庫源
方法2、經過建立證書自帶的TLS認證dom
5.一、生成自簽名證書 [root@otrs004097 ~]# mkdir -p /opt/docker/registry/certs [root@otrs004097 ~]# openssl req -newkey rsa:4096 -nodes -sha256 -keyout /opt/docker/registry/certs/domain.key -x509 -days 365 -out /opt/docker/registry/certs/domain.crt Generating a 4096 bit RSA private key .....................++ .....++ writing new private key to '/opt/docker/registry/certs/domain.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:shanghai Locality Name (eg, city) [Default City]:shanghai Organization Name (eg, company) [Default Company Ltd]:kj Organizational Unit Name (eg, section) []:it Common Name (eg, your name or your server's hostname) []: Email Address []: [root@otrs004097 ~]# ll /opt/docker/registry/certs/ total 8 -rw-r--r-- 1 root root 1944 Aug 20 11:04 domain.crt -rw-r--r-- 1 root root 3272 Aug 20 11:04 domain.key 5.2.建立帶有TLS認證的registry容器 [root@otrs004097 ~]# docker run -d --name registry2 -p 5000:5000 -v /opt/docker-registry/:/var/lib/registry -v /opt/docker/registry/certs:/certs -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt -e REGISTRY_HTTP_TLS_KEY=/certs/domain.key registry:2 0ae56ecfbcd015e59503f01ec8e3f52143753d1137aab530a823d1461b989a01 5.三、經過瀏覽器輸入進行訪問 6.[root@otrs004097 certs.d]# curl -X GET https://192.168.4.97:5000/v2/ -k {}
5.四、在其餘服務器進行上傳鏡像。 [root@DEV004019 ~]# docker push 192.168.4.97:5000/lqb_nginx:v1 The push refers to repository [192.168.4.97:5000/lqb_nginx] fe6a7a3b3f27: Pushed d0673244f7d4: Pushed d8a33133e477: Pushed v1: digest: sha256:dc85890ba9763fe38b178b337d4ccc802874afe3c02e6c98c304f65b08af958f size: 948 5.五、查看上傳的鏡像 [root@otrs004097 certs.d]# curl -X GET https://192.168.4.97:5000/v2/_catalog -k {"repositories":["lqb_nginx"]}
六、把須要上傳的鏡像,修改tag,爲本地鏡像,而後push。
[root@otrs004097 ~]# docker tag lqb2:v2.0 localhost:5000/lqb2v1:v1 [root@otrs004097 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE localhost:5000/yz v1.0 0011d86948f4 26 hours ago 346MB localhost:5000/yzv1 v1 0011d86948f4 26 hours ago 346MB lqb2 v2.0 62226ff8a5bc 27 hours ago 346MB localhost:5000/lqb2v1 v1 62226ff8a5bc 27 hours ago 346MB t1 latest ee097386456f 27 hours ago 380MB [root@otrs004097 ~]# docker push localhost:5000/lqb2v1:v1 The push refers to repository [localhost:5000/lqb2v1] fd214f756b32: Mounted from yzv1 v1: digest: sha256:dc7415d74223057a91d6525473e0aa7e1a8edd89ea63e9ec2166b2deeccb4fe2 size: 529
七、打開瀏覽器輸入:如下,會顯示上傳的鏡像列表
http://192.168.4.97:5000/v2/_catalog {"repositories":["lqb2v1","myubuntu","yzv1"]} [root@otrs004097 ~]# curl -XGET HTTP://192.168.4.97:5000/v2/_catalog {"repositories":["lqb2v1","myubuntu","yzv1"]}
八、測試下載鏡像,首先刪除鏡像,而後在下載
[root@otrs004097 ~]# docker rmi localhost:5000/lqb2v1:v1 Untagged: localhost:5000/lqb2v1:v1 Untagged: localhost:5000/lqb2v1@sha256:dc7415d74223057a91d6525473e0aa7e1a8edd89ea63e9ec2166b2deeccb4fe2 [root@otrs004097 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE localhost:5000/yzv1 v1 0011d86948f4 27 hours ago 346MB localhost:5000/yz v1.0 0011d86948f4 27 hours ago 346MB lqb2 v2.0 62226ff8a5bc 27 hours ago 346MB t1 latest ee097386456f 27 hours ago 380MB lqb1 v1.0 add4aac9e719 27 hours ago 369MB [root@otrs004097 ~]# docker pull localhost:5000/lqb2v1:v1 v1: Pulling from lqb2v1 Digest: sha256:dc7415d74223057a91d6525473e0aa7e1a8edd89ea63e9ec2166b2deeccb4fe2 Status: Downloaded newer image for localhost:5000/lqb2v1:v1 localhost:5000/lqb2v1:v1 [root@otrs004097 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE localhost:5000/yz v1.0 0011d86948f4 27 hours ago 346MB localhost:5000/yzv1 v1 0011d86948f4 27 hours ago 346MB lqb2 v2.0 62226ff8a5bc 27 hours ago 346MB localhost:5000/lqb2v1 v1 62226ff8a5bc 27 hours ago 346MB pull和push都正常上傳下載了 備註: docker push <registry_ip>:5000/<image_name>:<version>;上傳鏡像至私有倉庫 docker pull <registry_ip>:5000/<image_name>:<version>;從私有倉庫pull鏡像 docker run -d --name registry2 -p 5000:5000 -v /opt/docker-registry/:/var/lib/registry -v /opt/dcerts/:/certs -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt -e REGISTRY_HTTP_TLS_KEY=/certs/domain.key registry:2