创建 Docker 私有仓库
1 2 3 4 5 # docker pull registry# docker run -d -p 5000:5000 --name docker-registry --restart =always -v /container/registry:/var/lib/registry registry
查看私有仓库列表
1 2 3 4 5 6 7 # curl -X GET http://127.0.0.1:5000/v2/_catalog{"repositories" :["clay-tomcat" ]} # curl -X GET http://127.0.0.1:5000/v2/clay-tomcat/tags/list{"name" :"clay-tomcat" ,"tags" :["1.0" ]}
Push 本地镜像到私有仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 # docker pull tomcat# docker imagesEPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest 7ee26c09afb3 3 days ago 462MB # docker tag 7ee26c09afb3 127.0.0.1:5000/clay-tomcat:1.0# docker push 127.0.0.1:5000/clay-tomcat:1.0# curl -X GET http://127.0.0.1:5000/v2/_catalog{"repositories" :["clay-tomcat" ]}
Pull 私有仓库中的镜像到本地
1 2 3 4 5 6 7 8 9 # docker rmi tomcat# docker rmi 127.0.0.1:5000/clay-tomcat:1.0# docker pull 127.0.0.1:5000/clay-tomcat:1.0# docker run -d --name tomcat -p 8080:8080 127.0.0.1:5000/clay-tomcat:1.0
删除私有仓库中的镜像
Docker 官方不建议直接删除镜像的镜像层数据,所以没有接口直接删除镜像;删除镜像会很麻烦,一般如果删除某镜像只需删除该镜像的元数据,也就是 curl 命令查看到的镜像信息,而对于该镜像的镜像层数据需要进行垃圾回收才会真的被删除。如果删除的镜像与未删除的镜像公用了一些镜像层数据,垃圾回收之后再也用不了这些镜像了,因此删除元数据就好。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 # docker exec -it docker-registry /bin/sh# rm -rf /var/lib/registry/docker/registry/v2/repositories/clay-tomcat# docker exec registry bin/registry garbage-collect /etc/docker/registry/config.yml# docker restart docker-registry# curl -X GET http://127.0.0.1:5000/v2/_catalog{"repositories" :[]}
访问远程私有仓库
访问远程私有仓库之前,必须确认远程私有仓库所在服务器的防火墙开放了 Registry 端口(例如 5000)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 # curl -X GET http://192.168.1.130:5000/v2/_catalogError response from daemon: Get https://192.168.1.130:5000/v2/: http: server gave HTTP response to HTTPS client # vim /etc/docker/daemon.json{ "insecure-registries" :["192.168.1.130:5000" ] } # systemctl daemon-reload# systemctl restart docker# docker pull 192.168.1.130:5000/clay-tomcat:1.0# docker pull hello-world# docker tag fce289e99eb9 192.168.1.130:5000/hello-world:1.0# docker push 192.168.1.130:5000/hello-world:1.0# curl -X GET http://192.168.1.130:5000/v2/_catalog
创建带身份验证的 Docker 私有仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 # docker pull registry# mkdir -p /etc/docker/registry-auth# docker run --entrypoint htpasswd registry:latest -Bbn clay 123456 >> /etc/docker/registry-auth/htpasswd# cat /etc/docker/registry-auth/htpasswdclay:$2y $05 $nkzz4O9BARoZb8O61WHmLelm29GI /qOv3gUKimy5aTtDvm1tmg30e # docker run -p 5000:5000 --restart always --name docker-registry \ -v /container/registry:/var/lib/registry \ -v /etc/docker/registry-auth:/auth \ -e "REGISTRY_AUTH=htpasswd" \ -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \ -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \ -d registry# docker login --username =clay 127.0.0.1:5000# curl -X GET -u clay:123456 http://127.0.0.1:5000/v2/_catalog# docker pull 127.0.0.1:5000/clay-tomcat:1.0