docker私有镜像仓库搭建

  环境:centos7,dockere版本:18.09.0,镜像仓库:v2 

  docker-registry:192.168.137.101   docker私有仓库服务器

  docker-app: 192.168.137.102   运行docker的普通机器

  

一、安装私有仓储

  1. 登录docker私有仓库服务器
  2. 创建私有仓库存储的文件夹,make -p /docker/registry
  3. 执行以下命令,会启动一个registry容器,该容器用于提供私有仓库的服务
docker run -d -p 5000:5000 --name registry-ser --restart=always 
--privileged=true -v /docker/registry:/var/lib/registry registry
  • --restart=always 表示自动启动容器
  • -v <宿主机目录>:<容器目录> 将宿主机的目录映射到容器上
  • --privileged=true 给容器加权限,这样上传就不会因为目录权限出错
  • /var/lib/registry 这个目录是docker私有仓库,镜像的存储目录

--restart的参数说明 
# always:无论容器的退出代码是什么,Docker都会自动重启该容器。 
# on-failure:只有当容器的退出代码为非0值的时候才会自动重启。
#另外,该参数还接受一个可选的重启次数参数,`--restart=on-fialure:5`表示当容器退出代码为非0时,Docker#会尝试自动重启该容器,最多5次。

二、测试

1.接下来,把本地的hello-world镜像push到私有仓储。

docker pull hello-world

2.接下来修改该镜像的tag。

docker tag hello-world 192.168.137.101:5000/hello-world

3.接下来把打了tag的镜像上传到私有仓库。

docker push 192.168.137.101:5000/hello-world

很不幸,这里报错了。因为从docker1.3.2版本开始,使用registry时,必须使用TLS保证其安全。

1月 05 22:32:38 localhost.localdomain dockerd[3449]: time="2019-01-05T22:32:38.578520907+08:00" 
level=error msg="Handler for GET /v1.39/images/search returned error: invalid registry endpoint
https://192.168.137.101:5000/v1/: Get https://192.168.137.101:5000/v1/_ping: http: server gave
HTTP response to HTTPS client. If this private registry supports only HTTP or HTTPS with an unknown CA certificate,
please add `--insecure-registry 192.168.137.101:5000` to the daemon's arguments. In the case of HTTPS,
if you have access to the registry's CA certificate, no need for the flag; simply place the CA certificate
at /etc/docker/certs.d/192.168.137.101:5000/ca.crt"

  最简单的办法是,在需要连接该私有仓库的所有客户端docker宿主机上,修改daemon.json配置文件,增加insecure-registry参数。

[root@localhost ~]# vi /etc/docker/daemon.json 

{
  "registry-mirrors": ["https://yvaq2qqy.mirror.aliyuncs.com"],
  "insecure-registries": ["192.168.137.101:5000"]
}

  或者直接在 /lib/systemd/system/docker.service 这个启动文件里加上 --insecure-registry 192.168.137.101:5000

[root@localhost system]# cat -n /lib/systemd/system/docker.service 
     1  [Unit]
     2  Description=Docker Application Container Engine
     3  Documentation=https://docs.docker.com
     4  BindsTo=containerd.service
     5  After=network-online.target firewalld.service
     6  Wants=network-online.target
     7
     8  [Service]
     9  Type=notify
    10  # the default is not to use systemd for cgroups because the delegate issues still
    11  # exists and systemd currently does not support the cgroup feature set required
    12  # for containers run by docker
    13  ExecStart=/usr/bin/dockerd -H unix:// 
    14  --insecure-registry 192.168.137.101:5000   #加上这一句
    15  ExecReload=/bin/kill -s HUP $MAINPID
    16  TimeoutSec=0
    17  RestartSec=2
    18  Restart=always

  之后重启docker服务:systemctl restart docker

  之后再push,发现成功了。

[root@localhost system]# docker push 192.168.137.101:5000/hello-world
The push refers to repository [192.168.137.101:5000/hello-world]
af0b15c8625b: Pushed 
latest: digest: sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a size: 524

三、管理私有镜像

  在Private Registry 2中查看或检索Repository或images,将不能用docker search。

  但通过v2版本的API,我们可以实现相同目的,必须按照IP:port/v2/_catalog格式:

  curl http://192.168.137.101:5000/v2/_catalog 、curl http://192.168.137.101:5000/v2/hello-world/tags/list

  查看私有仓库的信息。

[root@localhost system]# curl http://192.168.137.101:5000/v2/_catalog
{"repositories":["hello-world"]}

[root@localhost system]# curl http://192.168.137.101:5000/v2/hello-world/tags/list
{"name":"hello-world","tags":["latest"]}

#从私有仓储里拉取hello-world镜像
[root@localhost system]# docker pull 192.168.137.101:5000/hello-world
Using default tag: latest
latest: Pulling from hello-world
Digest: sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a
Status: Image is up to date for 192.168.137.101:5000/hello-world:latest
 
原文地址:https://www.cnblogs.com/sdadx/p/10021358.html