1、建立Docker Registry服務器php
[root@localhost ~]# docker run -d -p 5000:5000 --restart=always --name registry -v /opt/data/registry:/var/lib/registry registry:2 Unable to find image 'registry:2' locally 2: Pulling from library/registry cbdbe7a5bc2a: Pull complete 47112e65547d: Pull complete 46bcb632e506: Pull complete c1cc712bcecd: Pull complete 3db6272dcbfa: Pull complete Digest: sha256:8be26f81ffea54106bae012c6f349df70f4d5e7e2ec01b143c46e2c03b9e551d Status: Downloaded newer image for registry:2 9f41b7101b2a0b3e5a32edfc13ad6877a7db6821a4ccc639ddfbc8e2ef819b21 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE registry 2 2d4f4b5309b1 2 months ago 26.2MB [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9f41b7101b2a registry:2 "/entrypoint.sh /etc…" 58 seconds ago Up 57 seconds 0.0.0.0:5000->5000/tcp registry [root@localhost ~]# docker tag apache2.4.25:centos7.8 192.168.146.199:5000/images/apache2.4.25:centos7.8 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 192.168.146.199:5000/images/apache2.4.25 centos7.8 1c65ef06567d 11 days ago 362MB apache2.4.25 centos7.8 1c65ef06567d 11 days ago 362MB registry 2 2d4f4b5309b1 2 months ago 26.2MB [root@localhost ~]# docker push 192.168.146.199:5000/images/apache2.4.25:centos7.8 The push refers to repository [192.168.146.199:5000/images/apache2.4.25] Get https://192.168.146.199:5000/v2/: http: server gave HTTP response to HTTPS client docker registry交互默認使用的是HTTPS,可是搭建私有鏡像默認使用的是HTTP服務,因此與私有鏡像交時出現以上錯誤。 從錯誤信息來看,client與Registry交互,默認將採用https訪問,但咱們在install Registry時並未配置指定任何tls相關的key和crt文件,https訪問定然失敗。要想弄清這個問題,只能查看Registry Manual。
2、Insecure Registrynode
不建議使用這種方式,不安全,並且這種方式每一個docker daemon都須要配置 [root@localhost ~]# cat /etc/docker/daemon.json { "registry-mirrors": ["https://rmjzc498.mirror.aliyuncs.com"], "insecure-registries": ["192.168.146.199:5000"] } [root@localhost ~]# systemctl daemon-reload [root@localhost ~]# systemctl restart docker.service 訪問出現{}正常 [root@localhost ~]# curl http://192.168.146.199:5000/v2/ {}[root@mydockerhub ~]# 訪問:http://192.168.146.199:5000/v2/ [root@mydockerhub ~]# docker info Insecure Registries: 192.168.146.199:5000 127.0.0.0/8 [root@localhost ~]# docker push 192.168.146.199:5000/images/apache2.4.25:centos7.8 The push refers to repository [192.168.146.199:5000/images/apache2.4.25] f12518ead8c9: Pushed 585d052d1d0b: Pushed 613be09ab3c0: Pushed centos7.8: digest: sha256:a65eec8cd9ba043f6b327b7e1163055ed6e4e85afe8249e40150de0bb8c1653f size: 953 查看所有鏡像 [root@mydockerhub ~]# curl -XGET http://192.168.146.199:5000/v2/_catalog 查看指定鏡像 [root@mydockerhub ~]# curl -XGET http://192.168.146.199:5000/v2/images/apache2.4.25/tags/list {"name":"images/apache2.4.25","tags":["centos7.8"]} 測試拉取 [root@mydockerhub ~]# docker rmi ubuntu:latest Untagged: ubuntu:latest Untagged: ubuntu@sha256:31dfb10d52ce76c5ca0aa19d10b3e6424b830729e32a89a7c6eee2cda2be67a5 Deleted: sha256:4e2eef94cd6b93dd4d794c18b45c763f72edc22858e0da5b6e63a4566a54c03c Deleted: sha256:160004bdd9a2800d0085be0315b769a9ce04c07ca175ecae89593eeee9aeb944 Deleted: sha256:9ed638911072c3379e75d2eaf7c2502220d6757446325c8d96236410b0729268 Deleted: sha256:ce7da152e578608030e9a05f9f5259b329fe5dcc5bf48b9f544e48bd69a5f630 Deleted: sha256:2ce3c188c38d7ad46d2df5e6af7e7aed846bc3321bdd89706d5262fefd6a3390 [root@mydockerhub ~]# docker images [root@mydockerhub ~]# docker pull 192.168.146.199:5000/ubuntu:latest latest: Pulling from ubuntu 54ee1f796a1e: Pull complete f7bfea53ad12: Pull complete 46d371e02073: Pull complete b66c17bbf772: Pull complete Digest: sha256:6f2fb2f9fb5582f8b587837afd6ea8f37d8d1d9e41168c90f410a6ef15fa8ce5 Status: Downloaded newer image for 192.168.146.199:5000/ubuntu:latest 192.168.146.199:5000/ubuntu:latest [root@mydockerhub ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 192.168.146.199:5000/ubuntu latest 4e2eef94cd6b 2 weeks ago 73.9MB
3、Secure Registrydocker
一、自行申請證書apache
Docker官方是推薦你採用Secure Registry的工做模式的,即transport採用tls。這樣咱們就須要爲Registry配置tls所需的key和crt文件了。json
若是你擁有一個域名,域名下主機提供Registry服務,而且你擁有某知名CA簽署的證書文件,那麼你能夠創建起一個Secure Registry。不過我這裏沒有現成的證書,只能使用自簽署的證書。ubuntu
嚴格來說,使用自簽署的證書在Docker官方眼中依舊屬於Insecure,不過這裏只是藉助自簽署的證書來講明一下Secure Registry的部署步驟罷了。centos
先將/etc/docker/daemon.json配置文件恢復,而後重啓docker服務。安全
[root@localhost ~]# mkdir -p /opt/certs [root@localhost ~]# cd /opt/certs [root@localhost certs]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout certs/domain.key -x509 -days 365 -out domain.crt Generating a 2048 bit RSA private key ....................................................................................+++ ..................................................................................................................................................+++ writing new private key to '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) []:GuangDong Locality Name (eg, city) [Default City]:ShenZhen Organization Name (eg, company) [Default Company Ltd]:mydockerhub.com Organizational Unit Name (eg, section) []:mydockerhub.com Common Name (eg, your name or your server's hostname) []:mydockerhub.com Email Address []: [root@localhost ~]# ll -rw-r--r--. 1 root root 1395 Sep 1 22:10 certs/domain.crt -rw-r--r--. 1 root root 1704 Sep 1 22:10 certs/domain.key 或者從代理商申請證書,比較有名的有GoDaddy.com、GlobalSign.com等。
二、啓動Secure Registrybash
啓動帶證書的Registry [root@localhost certs]# docker run -d -p 5000:5000 --restart=always --name registry -v /opt/data/registry:/var/lib/registry -v /opt/certs:/certs -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt -e REGISTRY_HTTP_TLS_KEY=/certs/domain.key registry:2 Unable to find image 'registry:2' locally 2: Pulling from library/registry cbdbe7a5bc2a: Pull complete 47112e65547d: Pull complete 46bcb632e506: Pull complete c1cc712bcecd: Pull complete 3db6272dcbfa: Pull complete Digest: sha256:8be26f81ffea54106bae012c6f349df70f4d5e7e2ec01b143c46e2c03b9e551d Status: Downloaded newer image for registry:2 f7f26e6010a53c2d90d077529dd3bcdf802910c3de7697243e36cda8965ac534 [root@mydockerhub ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 002c15d2613b registry:2 "/entrypoint.sh /etc…" 2 seconds ago Up 1 second 0.0.0.0:5000->5000/tcp registry [root@localhost certs]# cd [root@localhost ~]# cd /etc/hosts 192.168.146.199 mydockerhub.com [root@localhost ~]# docker tag php7.2.10:centos7.8 mydockerhub.com:5000/images/php7.2.10:centos7.8 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mydockerhub.com:5000/images/php7.2.10 centos7.8 4b286eb8be58 12 days ago 576MB [root@localhost ~]# docker push mydockerhub.com:5000/images/php7.2.10:centos7.8 The push refers to repository [mydockerhub.com:5000/images/php7.2.10] Get https://mydockerhub.com:5000/v2/: x509: certificate signed by unknown authority 推送鏡像報錯,docker client認爲server傳輸過來的證書的簽署方是一個unknown authority(未知的CA),所以驗證失敗。咱們須要讓docker client安裝咱們的CA證書: 備註:若是使用自簽署的證書,那麼全部要與Registry交互的Docker主機都須要安裝mydockerhub.com的ca.crt(domain.crt)。但若是你使用知名CA,這一步也就能夠忽略。 解決辦法: [root@localhost certs]# mkdir -p /etc/docker/certs.d/mydockerhub.com:5000 [root@localhost certs.d]# cp /opt/certs/domain.crt /etc/docker/certs.d/mydockerhub.com:5000/ca.crt [root@localhost certs.d]# systemctl restart docker 再次推送成功 [root@localhost certs.d]# docker push mydockerhub.com:5000/images/php7.2.10:centos7.8 The push refers to repository [mydockerhub.com:5000/images/php7.2.10] 6d5bd35f0349: Pushed 07fbf9f3f5e1: Pushed 613be09ab3c0: Pushed centos7.8: digest: sha256:bf4bcd4aec0fcc810b1b9980e68e57cf464cce64e1ab83e37c66fb594c4301a0 size: 954
4、Registry的鑑權管理服務器
[root@localhost ~]# yum -y install httpd [root@localhost ~]# mkdir -p /opt/auth #備註20200901記錄:registry:2沒有htpasswd命令,registry:2.5有,坑人。 #備註20200901記錄:若是直接調用htpasswd命令生成,好像有問題(能夠嘗試-Bbn參數試下),多是加密方式不同。 [root@localhost ~]# docker run --entrypoint htpasswd registry:2.5 -Bbn admin 1qaz2wsx > /opt/auth/htpasswd [root@localhost ~]# cat /opt/auth/htpasswd test:$apr1$kty2DTdF$xSyaidmfpSu1aOLxqh04Q/ admin:$2y$05$OVNLmtFX1MR/Tjv.xRb64eHXHtOrNeLiiMTynH6o1oy1***3Abszq 啓動帶證書的Registry [root@localhost certs]# docker run -d -p 5000:5000 --restart=always --name registry -v /opt/auth:/auth -v /opt/data/registry:/var/lib/registry -v /opt/certs:/certs -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt -e REGISTRY_HTTP_TLS_KEY=/certs/domain.key -e REGISTRY_AUTH=htpasswd -e REGISTRY_AUTH_HTPASSWD_REALM=RegistryRealm -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd registry:2 [root@mydockerhub ~]# docker login mydockerhub.com:5000 Username: admin Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded [root@mydockerhub ~]# docker push mydockerhub.com:5000/images/php7.2.10:centos7.8 6d5bd35f0349: Preparing 07fbf9f3f5e1: Preparing 613be09ab3c0: Preparing centos7.8: digest: sha256:bf4bcd4aec0fcc810b1b9980e68e57cf464cce64e1ab83e37c66fb594c4301a0 size: 954
5、Registry中images的管理
前面提到過,經過V2版Rest API能夠查詢Repository和images:
[root@mydockerhub ~]# curl --cacert /opt/certs/domain.crt --basic --user admin:1qaz2wsx https://mydockerhub.com:5000/v2/_catalog
但若是要刪除Registry中的Repository或某個tag的Image,目前v2還不支持,緣由見Registry的roadmap中的說明。
不過若是你的Registry的存儲引擎使用的是本地盤,卻是有一些第三方腳本可供使用,好比:delete-docker-registry-image。
Registry2發佈不到1年,目前還有許多問題待解決,就好比delete image的問題,相信在2.4以及後續版本這些問題會被逐個解決掉或能找到一個相對理想的方案。