ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

13.docker私服registry搭建

2026/9/15 21:54:35 拓冰建站 浏览量
13.docker私服registry搭建

容器注册

Docker Hub是最大、使用最广泛的docker容器注册托管中心。

  • registry用于保存docker镜像,包括镜像的层次结构和元数据。
  • 启动容器时,docker daemon会试图从本地获取相关的镜像;本地镜像不存在时,其将从registry中下载该镜像并保存到本地;
  • 拉取镜像时,如果不知道registry仓库地址,默认从Docker Hub搜索拉取镜像

我们可以利用docker自己搭建私有registry。一般私有registry不像docker hub功能齐全,而是只提供了基础功能,比较适合小团队。

启动私有docker registry

启动docker registry

docker container run -d -p 5000:5000 --name registry registrydocker container ls
# CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS                  PORTS                    NAMES
# d88855125dd4   registry      "/entrypoint.sh /etc…"   1 second ago     Up Less than a second   0.0.0.0:5000->5000/tcp   registry

重新给镜像打标签,并且推送到私有registry

docker pull hello-worlddocker run --name hello hello-world docker tag hello-world 127.0.0.1:5000/hello-worlddocker image lsdocker push 127.0.0.1:5000/hello-worlddocker container rm hellodocker image remove hello-worlddocker image remove 127.0.0.1:5000/hello-worlddocker image ls

从本地删除对应镜像缓存

从私有registry尝试拉取镜像

docker pull 127.0.0.1:5000/hello-worlddocker image ls

将私有registry的数据持久化到宿主机

docker container kill registrydocker container rm registrydocker container run -d -p 5000:5000 --name registry -v $(pwd)/registry-date:/var/lib/registry registryls -aldocker push 127.0.0.1:5000/hello-worldls -altree registry-date

使用Swarm将registry部署到线上

使用swarm部署registry服务,利用routing mesh特性

我们先部署5个manager节点,

docker node lsdocker service create --name registry --publish 5000:5000 registrydocker service ps registry
# ID             NAME         IMAGE             NODE       DESIRED STATE   CURRENT STATE            ERROR     PORTS
# jfwgt7gmwu62   registry.1   registry:latest   manager1   Running         Running 13 seconds ago  

利用routing mesh特性,我们可以在任意节点上从私有registry拉取镜像

curl localhost:5000/v2/_catalog
# {"repositories":[]}docker pull hello-worlddocker tag hello-world 127.0.0.1:5000/hello-worlddocker push 127.0.0.1:5000/hello-worldcurl localhost:5000/v2/_catalog
# {"repositories":["hello-world"]}docker pull nginxdocker tag nginx 127.0.0.1:5000/nginxdocker push 127.0.0.1:5000/nginxcurl localhost:5000/v2/_catalogdocker service create --name nginx -p 80:80 --replicas 5 --detach=false 127.0.0.1:5000/nginxdocker service ps nginx

可以看到5个节点都成功拉取到了私服的镜像