docker 11 :私有仓库搭建

部署私有仓库可以解决以下问题:
  1) 下载镜像时所带来的网络延时。
  2) 方便镜像版本的更新与维护
  3)部署私有应用程序

一、搭建私有仓库

挂载宿主机/opt/myregistry目录到容器目录/var/lib/registry

[root@docker01 ~]# docker run -d -p 5000:5000 --restart=always --name registry -v /opt/myregistry:/var/lib/registry registry

 

二、上传本地镜像到私有仓库

[root@localhost /]# docker images
REPOSITORY                   TAG                  IMAGE ID            CREATED             SIZE
centos                       latest               300e315adb2f        4 months ago        209MB

[root@localhost /]# docker push localhost:5000/centos:latest
The push refers to repository [localhost:5000/centos]
An image does not exist locally with the tag: localhost:5000/centos

  这边有收到一个报错。

An image does not exist locally with the tag: localhost:5000/centos
 
原因是没有指定镜像要上传的地址,站点。默认的是docker.io
解决办法是需要标记本地镜像,将镜像归入某一个仓库。

 docker tag : 标记本地镜像,将其归入某一仓库。

  语法

docker tag [OPTIONS] IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]

[root@localhost /]# docker tag centos:latest localhost:5000/centos8:v1.0 

  然后我们再push一次,于是就遇到第二个报错:

[root@localhost /]# docker push localhost:5000/centos8:v1.0 
The push refers to repository [localhost:5000/centos7]
Get https://localhost:5000/v2/: http: server gave HTTP response to HTTPS client

  报错:Get https://localhost:5000/v2/: http: server gave HTTP response to HTTPS client

  这是因为Docker自从1.3.X之后docker registry交互默认使用的是HTTPS,但是搭建私有镜像默认使用的是HTTP服务,所以与私有镜像交时出现错误。

  解决办法有两种,一种是在docker server启动的时候,增加启动参数,默认使用HTTP访问:

vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd --insecure-registry localhost:5000

  另外一种是修改daemon.json文件。如下:

vim  /etc/docker/daemon.json
{
"insecure-registries":["localhost:5000"]
}

  然后重启docker服务:

   systemctl daemon-reload

   systemctl restart docker

  然后再上传就可以了:

[root@localhost /]# docker push localhost:5000/centos8:v1.0  
The push refers to repository [localhost:5000/centos8]
2653d992f4ef: Pushed 
v1.0: digest: sha256:dbbacecc49b088458781c16f3775f2a2ec7521079034a7ba499c8b0bb7f86875 size: 529

  查看上传的镜像:

[root@localhost /]# curl localhost:5000/v2/_catalog
{"repositories":["centos8"]}

  由于我们挂载了本地磁盘,所以在本地也可以看到。

[root@localhost opt]# cd /opt/myregistry/docker/registry/v2/repositories/
[root@localhost repositories]# ls
centos8

  

三、删除镜像

  1)进入docker registry 的容器中

[root@docker01 ~]# docker exec -it registry /bin/sh

  2) 删除指定镜像目录文件

/ # rm -fr /var/lib/registry/docker/registry/v2/repositories/centos8

  3) 清理掉blob

/ # registry garbage-collect /etc/docker/registry/config.yml

  4)     再次查看版本库

[root@master mnt]#  curl http://localhost:5000/v2/_catalog

{"repositories":[]}

  

 
 

原文地址:https://www.cnblogs.com/tortoise512/p/14677636.html