构建 Docker registry 私有镜像,解决docker私有仓库push出错问题

时间:2022-06-19
本文章向大家介绍构建 Docker registry 私有镜像,解决docker私有仓库push出错问题,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

适用于:Docker 1.8      环境:Centos7

每个版本的docker修改配置内容都不同,请大家看好安装的docker版本。

1)下载 docker registry私有镜像

docker pull docker.io/registry

2)修改配置文件

gedit /etc/sysconfig/docker

OPTIONS=’–selinux-enabled --insecure-registry 192.168.137.17:5000’

注:IP为修改的部分

3)从容器中启动Registry

docker run -d -p 5000:5000 --name registry -v /home/dockeruser/data:/tmp/registry  docker.io/registry

注:-d 表示该容器在后台运行

-p将主机的5000端口与容器的5000端口映射

–name 将容器的名称命名为registry

-v 表示将镜像的存储位置“/tmp/registry” 变更为宿主机的“/home/dockeruser/data”

注:这一步一定要在步骤2)之后,否则修改配置文件不会生效

4)在上传到私有的registry之前,需要指定新的Registry目的地址,需要在镜像名前加上

主机名和端口的前缀。

#docker images

找到要上传镜像的id,添加标签为192.168.137.17:5000/helloworld这种形式

docker tag id 192.168.137.17:5000/helloworld

注:192.168.137.17:5000 为所在主机的IP地址和端口

5)将打标签的镜像上传到私有库

docker push 192.168.137.17:5000/helloworld

显示信息如下:

The push refers to a repository [192.168.137.17:5000/node/helloworld] (len: 1)

Sending image list

Pushing repository 192.168.137.17:5000/node/helloworld (1 tags)
d8bd0657b25f: Image successfully pushed
Pushing tag for rev [df45579dbc81] on
{http://192.168.137.17:5000/v1/repositories/node/helloworld/tags/latest}

遇到问题:

The push refers to a repository [192.168.137.17:5000/node/helloworld] (len: 1) unable to ping registry endpoint https://192.168.137.17:5000/v0/ v2 ping attempt failed with error: Get https://192.168.137.17:5000/v2/: dial tcp 192.168.137.17:5000: connection refused v1 ping attempt failed with error: Get https://192.168.137.17:5000/v1/_ping: dial tcp 192.168.137.17:5000: connection refused

解决方案:

gedit /etc/sysconfig/docker

OPTIONS=’–selinux-enabled --insecure-registry 192.168.137.17:5000’

注:这一步一定要在创建私有仓库的容器之前【步骤2)】,否则修改配置文件不会生效

[root@node ~]# docker push 172.18.18.90:5000/busybox:v1 The push refers to repository [172.18.18.90:5000/busybox] Get https://172.18.18.90:5000/v2/: http: server gave HTTP response to HTTPS client

注意了,这是报错了,需要https的方法才能上传,我们可以修改下daemon.json来解决:

[root@node ~]# vim /etc/docker/daemon.json 
{
  "registry-mirrors": [ "https://registry.docker-cn.com"],
  "insecure-registries": [ "172.18.18.90:5000"]
}

添加私有镜像服务器的地址,注意书写格式为json,有严格的书写要求,然后重启docker服务:

[root@node ~]# systemctl  restart docker

参考: https://blog.csdn.net/chen798213337/article/details/51046175 https://www.linuxidc.com/Linux/2018-03/151308.htm