Docker-07-私有仓库搭建

359 阅读3分钟

「这是我参与11月更文挑战的第8天,活动详情查看:2021最后一次更文挑战」。

某些时候使用 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            "/entrypoint.sh /etc…"   5 seconds ago       Up 2 seconds        0.0.0.0:5000->5000/tcp       my_registry
root@phyger-VirtualBox:/home/phyger#

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

访问私有仓库

查询 host 的地址:

host的地址

从本地 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 查询私有仓库中的镜像:

查询私有仓库中的镜像

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

my_ubuntu镜像

拉取测试

查看本地镜像列表:

本地镜像列表

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

再次查看本地镜像

拉取 127.0.0.1:5000/my_ubuntu

拉取127.0.0.1:5000/my_ubuntu

补充

默认的私有仓库的镜像存放在容器的/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