Docker-07-私有仓库搭建_docker



某些时候使用Docker Hub这样的公共仓库可能不方便,或者在公司内网无法访问公网的时候,我们可以选择创建一个本地仓库供私人使用。


直接安装运行docker-registry


root@phyger-VirtualBox:/home/phyger# docker run -d -p 5000:5000 --restart=always --name my_registry registry
Unable to find image 'registry:latest' locally
latest: 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:latest
22a16661a027fc031ba7cab0d9915fbd0ea0829139263e91934055e2988b205c
root@phyger-VirtualBox:/home/phyger# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
22a16661a027 registry "/ /etc…" 5 seconds ago Up 2 seconds 0.0.0.0:5000->5000/tcp my_registry
root@phyger-VirtualBox:/home/phyger#


我们在启动registry容器的时候将容器的5000端口映射到了host的5000端口,因为默认的registry的服务端口是5000,此时我们使用host的5000端口就可以访问到本地的私有仓库。


访问私有仓库


查询host的地址:

Docker-07-私有仓库搭建_ubuntu_02


从本地windows电脑访问虚拟机的IP+5000端口:

[Administrator.LWAL3QZC7R46JQC] ➤ curl http://192.168.56.102:5000/v2/_catalog
{"repositories":[]}

到此,说明本地仓库搭建成功。


上传镜像


查看本地镜像:

root@phyger-VirtualBox:/home/phyger# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my_ubuntu v5 038342424332 4 days ago 141MB
registry latest 2d4f4b5309b1 4 days ago 26.2MB
alpine latest a24bb4013296 3 weeks ago 5.57MB
ubuntu latest 1d622ef86b13 2 months ago 73.9MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB
root@phyger-VirtualBox:/home/phyger#


将my_ubuntu:v5标记为私有镜像127.0.0.1:5000/my_ubuntu:

root@phyger-VirtualBox:/home/phyger# docker tag my_ubuntu:v5 127.0.0.1:5000/my_ubuntu:v5
root@phyger-VirtualBox:/home/phyger# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my_ubuntu v5 038342424332 4 days ago 141MB
127.0.0.1:5000/my_ubuntu v5 038342424332 4 days ago 141MB
registry latest 2d4f4b5309b1 4 days ago 26.2MB
alpine latest a24bb4013296 3 weeks ago 5.57MB
ubuntu latest 1d622ef86b13 2 months ago 73.9MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB


将127.0.0.1:5000/my_ubuntu推送到本地私有仓库:

root@phyger-VirtualBox:/home/phyger# docker push 127.0.0.1:5000/my_ubuntu:v5
The push refers to repository [127.0.0.1:5000/my_ubuntu]
e92f897b912b: Pushed
v5: digest: sha256:8fa3f9d55d4e5793cf936a2592d2405bdf46796c9d06b54abcc1a8104c9a819f size: 528
root@phyger-VirtualBox:/home/phyger#


再次从本地windows查询私有仓库中的镜像:

Docker-07-私有仓库搭建_私有仓库_03


可以看到,本地私有仓库已经有了一个名为my_ubuntu的镜像,如果想查询my_ubuntu的详细信息,则使用如下方法:

Docker-07-私有仓库搭建_ubuntu_04


拉取测试


查看本地镜像列表:

Docker-07-私有仓库搭建_ubuntu_05


删除127.0.0.1:5000/my_ubuntu后再次查看本地镜像列表:

Docker-07-私有仓库搭建_docker_06


拉取​127.0.0.1:5000/my_ubuntu

Docker-07-私有仓库搭建_私有仓库_07


补充


默认的私有仓库的镜像存放在容器的/var/lib/registry路径下,如果想要指定镜像在host上的存放路径,则在启动registry的时候使用文件映射-v参数即可。

例如:

docker run -d -p 5000:5000 --restart=always -v /opt/data/my_registry /var/lib/registry --name my_registry registry


Docker-07-私有仓库搭建_docker_08

长按二维码

获取更多信息