一、搭建Docker Registry v2
1)预装好docker服务
yum install docker -y
systemctl enable docker
systemctl start docker
2)获取最新的registry的容器
了解到目前最新版为2.4.1,于是直接使用docker pull命令从公用仓库去拉即可
docker pull registry:2.4.1
3)运行registry:2.4.1容器
这里需要注意的是新registry仓库数据目录的位置。之前老版的位置是/tmp/registry,hub.docker.com上的演示命令里写的是/tmp/registry-dev,其实这个不对。试验证明,新registry的仓库目录是在/var/lib/registry,所以运行时挂载目录需要注意。
docker run -d -p 5000:5000 --restart=always -v /opt/registry-var/:/var/lib/registry/ registry:2.4.1
[root@localhost ~]# docker push 192.168.174.132:5000/registry:2.4.1
The push refers to a repository [192.168.1.23:5000/registry:2.4.1]
unable to ping registry endpoint https://192.168.174.132:5000/v0/
v2 ping attempt failed with error: Get https://192.168.174.132:5000/v2/: http: server gave HTTP response to HTTPS client
v1 ping attempt failed with error: Get https://192.168.174.132:5000/v1/_ping: http: server gave HTTP response to HTTPS client
出现上面错误的原因分析:
因为Docker从1.3.X之后,与docker registry交互默认使用的是https,然而此处搭建的私有仓库只提供http服务,所以当与私有仓库交互时就会报上面的错误。
为了解决这个问题需要在启动docker server时增加启动参数为默认使用http访问。
目前很多文章都是通过修改docker的配置文件“etc/systemconfig/docker”,重启docker来解决这个问题。
但发现docker1.12.3版本并无此文件,根据网上创建此文件,并填入相应内容,重启docker无效果,仍然报此错误。
解决方法:
1)在”/etc/docker/“目录下,创建”daemon.json“文件。在文件中写入:
{ “insecure-registries”:[“192.168.1.23:5000”] }
2)在 /etc/init/docker.conf 添加
ADD_REGISTRY=’–insecure-registry 192.168.174.132:5000’
我个人推荐第二种。
二、查看私有仓库里的镜像(一定要保证下面能查看到仓库里有镜像!如果仓库里没有镜像,那么客户端机器就无法从该私有仓库下载镜像了)
[root@localhost ~]# curl -XGET http://192.168.174.132:5000/v2/_catalog //即该私有仓库里有registry镜像
{“repositories”:[“centos”,”gab/hello-world”,”hello-mine”,”registry”]}
[root@localhost ~]# curl -XGET http://192.168.174.132:5000/v2/registry/tags/list
{“name”:”registry”,”tags”:[“2.4.1”]}
或者浏览器里访问(103.110.186.23是注册机的外网ip,iptables防火墙内开放5000端口访问):
http://103.110.186.23:5000/v2/_catalog
http://103.110.186.23:5000/v2/registry/tags/list
现在可以将本地的tomcat7和192.168.1.23:5000/registry:2.4.1镜像都删除,然后从私有仓库中下载
[root@localhost ~]# docker rmi registry:2.4.1
[root@localhost ~]# docker rmi 192.168.174.132:5000/registry:2.4.1
[root@localhost ~]# docker pull 192.168.174.132:5000/registry:2.4.1