(九)Docker私有仓库搭建

一、简介

仓库(Repository)是集中存放镜像的地方,又分为公共镜像和私有仓库。
当我们执行docker pull xxx的时候,它实际上是从registry.docker.com这个地址去查找,这就是Docker公司为我们提供的公共仓库。在工作中,我们不可能把企业项目上传到公共仓库进行管理,所以为了更多管理镜像,Docker允许我们搭建本地私有仓库。
私有仓库最常用的就是registry、Harbor两种,那接下来详细介绍如何搭建私有仓库。

二、搭建registry私有仓库

Docker 官方提供了一个搭建私有仓库的镜像registry,只需把镜像下载下来,运行容器并暴露5000端口,就可以使用了。代码如下:

1、下载镜像

docker pull registry:2

2、运行容器

docker run -d -v /opt/registry:/var/lib/registry -p 5000:5000 --name myregistry registry:2
registry服务默认将上传的镜像保存在容器的/var/lib/registry,使用-v参数将容器的/var/lib/registry目录映射到本地/opt/registry目录。即可实现将镜像保存到宿主机/opt/registry目录。
浏览器访问http://宿主机IP:5000/v2,显示“{}” 说明registry运行正常。

三、上传镜像到私有仓库

现在通过push将镜像上传至私有仓库,具体步骤如下:

1、下载docker hub官方镜像

docker pull ubuntu

2、将镜像标志为要推送到私有仓库:

docker tag ubuntu:latest 10.43.187.251:5000/myubuntu:v1

3、上传镜像到私有仓库

[root@qll251 ~]# docker push 10.43.187.251:5000/myubuntu:v1
The push refers to repository [10.43.187.251:5000/myubuntu]
Get https://10.43.187.251:5000/v2/: http: server gave HTTP response to HTTPS client
注意,上传镜像时报错了:http: server gave HTTP response to HTTPS client
出现这个问题原因是:Docker自从1.3.X之后docker registry交互默认使用的是HTTPS,但是搭建私有镜像默认使用的是HTTP服务,所以与私有仓库交互时出现以上错误。

解决办法:

1、编辑 /etc/docker/daemon.json,在文件中写入:

{ "insecure-registries":["10.43.187.251:5000"] }

2、重启生效:

systemctl daemon-reload
systemctl restart docker
再次上传,问题解决:
docker pull 10.43.187.251:5000/myubuntu:v1

四、客户端下载私有镜像

我们在另外一台docker机器上,进行pull测试:
[root@qll252 ~]# docker pull 10.43.187.251:5000/myubuntu:v1
Trying to pull repository 10.43.187.251:5000/myubuntu ...
Get https://10.43.187.251:5000/v1/_ping: http: server gave HTTP response to HTTPS client
会发现跟前面上传镜像报了同样的错误。
解决方法同上:

1、编辑/etc/docker/daemon.json配置文件,在文件中写入:

{"insecure-registries":["10.43.187.251:5000"]}

2、重启生效:

systemctl daemon-reload
Systemctl restart docker
再次从私有仓库中下载镜像:
[root@qll252 ~]# docker pull 10.43.187.251:5000/myubuntu:v1
Trying to pull repository 10.43.187.251:5000/myubuntu ...
v1: Pulling from 10.43.187.251:5000/myubuntu
d51af753c3d3: Pull complete
fc878cd0a91c: Pull complete
6154df8ff988: Pull complete
fee5db0ff82f: Pull complete
Digest: sha256:5747316366b8cc9e3021cd7286f42b2d6d81e3d743e2ab571f55bcd5df788cc8
Status: Downloaded newer image for 10.43.187.251:5000/myubuntu:v1
验证通过,到此已完成私有仓库的搭建工作了。
说明:在后续教程我们学习到docker-compose时,载为搭建介绍企业级harbor私有仓库及搭建过程。

posted @ 2022-05-24 09:32  比特边界  阅读(137)  评论(0编辑  收藏  举报