Docker私有仓库

本文详细介绍Docker私有仓库的搭建步骤,包括私有仓库的安装、使能http推送镜像、镜像的推送与从私有仓库拉取镜像的过程。适合希望了解并实践Docker私有仓库部署的技术人员。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 私有仓库安装

docker-registry 是官⽅提供的⼯具,可以⽤于构建私有的镜像仓库。可以通过获取官⽅ registry 镜像来运⾏。先安装好docker。执行如下命令安装

$ docker run -d -p 5000:5000 --restart=always --name registry registry

默认情况下,仓库会被创建在容器的 /var/lib/registry ⽬录下。你可以通过 -v 参数来将镜像⽂件存放在本地的指定路径。例如下⾯的例⼦将上传的镜像放到本地的 /opt/data/registry(若没有先创建) ⽬录。

$ docker run -d -p 5000:5000 -v /opt/data/registry:/var/lib/registry --restart=always --name registry registry
-d 后台运行
-p 5000:5000 前面是Docker主机的端口,后面是容器的端口,意为将容器的5000映射到主机的5000端口
-v 是一个bind mount,以为把主机的/opt/data/registry挂载到容器的/var/lib/registry目录
--restart=always Docker重启时容器自动启动
--name registry 容器起个名字

2. 使能http推送镜像

这是因为 Docker 默认不允许⾮ HTTPS ⽅式推送镜像。我们可以通过 Docker 的配置选项来取消这个
限制。
对于systemd 系统Centos7,打开客户端主机的daemon.json,vi /etc/docker/daemon.json添加"insecure-registries":["192.168.110.137:5000"]

{
    "registry-mirrors":["http://hub-mirror.c.163.com"],
    "insecure-registries":["192.168.110.137:5000"]
}

重启服务

systemctl daemon-reload
systemctl restart docker

对于upstart系统Ubuntu,编辑 /etc/default/docker ⽂件,在其中的 DOCKER_OPTS 中增加如下内
容:

DOCKER_OPTS="--registry-mirror=https://registry.docker-cn.com --insecure-registries=192.168.110.137:5000

重启服务

sudo service docker restart

3. push镜像

先拉去一个镜像,或者直接使用已经下载的镜像打tag,然后推送到仓库

  • 拉取最新的Ubuntu: docker pull ubuntu
  • 打TAG: docker tag ubuntu:latest 192.168.110.137:5000/ubuntu:latest
  • push到私有仓库: docker push 192.168.110.137:5000/ubuntu
  • 查看结果: curl 192.168.110.137:5000/v2/_catalog
[root@localhost registry]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
d51af753c3d3: Pull complete
fc878cd0a91c: Pull complete
6154df8ff988: Pull complete
fee5db0ff82f: Pull complete
Digest: sha256:747d2dbbaaee995098c9792d99bd333c6783ce56150d1b11e333bbceed5c54d7
Status: Downloaded newer image for ubuntu:latest
[root@localhost registry]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              1d622ef86b13        4 weeks ago         73.9MB
registry            latest              708bc6af7e5e        4 months ago        25.8MB
hello-world         latest              bf756fb1ae65        4 months ago        13.3kB
[root@localhost registry]# docker tag ubuntu:latest 192.168.110.137:5000/ubuntu:latest
[root@localhost registry]# docker image ls
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
192.168.110.137:5000/ubuntu   latest              1d622ef86b13        4 weeks ago         73.9MB
ubuntu                        latest              1d622ef86b13        4 weeks ago         73.9MB
registry                      latest              708bc6af7e5e        4 months ago        25.8MB
hello-world                   latest              bf756fb1ae65        4 months ago        13.3kB
[root@localhost registry]# docker push 192.168.110.137:5000/ubuntu
The push refers to repository [192.168.110.137:5000/ubuntu]
8891751e0a17: Pushed
2a19bd70fcd4: Pushed
9e53fd489559: Pushed
7789f1a3d4e9: Pushed
latest: digest: sha256:5747316366b8cc9e3021cd7286f42b2d6d81e3d743e2ab571f55bcd5df788cc8 size: 1152
[root@localhost registry]# curl 192.168.110.137:5000/v2/_catalog
{"repositories":["ubuntu"]}
[root@localhost registry]#

4. pull私有仓库的镜像

  • docker pull 192.168.110.137:5000/ubuntu:latest
[root@localhost docker]# docker pull 192.168.110.137:5000/ubuntu:latest
latest: Pulling from ubuntu
d51af753c3d3: Pull complete
fc878cd0a91c: Pull complete
6154df8ff988: Pull complete
fee5db0ff82f: Pull complete
Digest: sha256:5747316366b8cc9e3021cd7286f42b2d6d81e3d743e2ab571f55bcd5df788cc8
Status: Downloaded newer image for 192.168.110.137:5000/ubuntu:latest
[root@localhost docker]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
192.168.110.137:5000/ubuntu   latest              1d622ef86b13        4 weeks ago         73.9MB
hello-world                   latest              bf756fb1ae65        4 months ago        13.3kB
[root@localhost docker]#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值