Deploy docker private registry

本文详细介绍了如何在局域网内使用DockerDistribution部署私有DockerRegistry,包括配置、启动服务、防火墙设置、镜像的推送与拉取测试、镜像删除及垃圾回收等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
 

前言

前面已经学习了怎样通过Dockerfile来构建我们自己的镜像了,但是一个系统可能包含着很多个微服务即有很多个镜像,当镜像越来越多的时候,就必须得有一个地方来管理这些镜像,Docker官方提供了Docker Hub来维护管理所有的镜像,只是对于免费用户而言,只能创建一个私有仓库,付费用户才拥有更多私有仓库的权限,对此官方开源了Docker Registry的源代码,我们可以通过它在局域网内部搭建私有的镜像注册中心.

Docker 私用仓库部署方式:
1、基于docker自带的私有仓库registry
2、基于docker-distribution
本文采用docker-distribution部署
 
环境

System EditionHostnameIP
Centos7registry192.168.1.10
Centos7Client192.168.1.11

一、Deploy registry(1.10)

1、安装配置

[root@registry ~]# yum -y install registry
[root@registry ~]# vim /etc/docker-distribution/registry/config.yml
version: 0.1
log:
  fields:
    service: registry
storage:
    delete:
       enabled: true  # 允许删除镜像
    cache:
        layerinfo: inmemory
    filesystem:
        #rootdirectory: /var/lib/registry
        rootdirectory: /data/lib/registry  # Image storage path 
http:
    addr: :5000  # Service port

2、启动服务并查看服务

[root@registry ~]# systemctl start docker-distribution
[root@registry ~]# systemctl enable docker-distribution

3、配置firewall

firewall-cmd --permanent --add-rich-rule="rule family=ipv4 \
> source address=192.168.1.0/24 port port=5000 protocol=tcp accept"
[root@registry ~]# firewall-cmd --reload

二、Push and pull test(1.11)

1、修改上传image时使用的HTTP协议,默认为HTTPS

[root@client ~]# vim /etc/docker/daemon.json

{"registry-mirrors": ["http://hub-mirror.c.163.com"],
 "insecure-registries": ["registry.com:5000"]
                        }

注释: “registry-mirrors” 为镜像加速
             "insecure-registries"为不使用HTTPS
 

[root@client ~]# systemctl daemon-reload
[root@client ~]# systemctl restart docker

2、给image打 tag

[root@client ~]# docker commit -p test registry.com:5000/test:v1	#从container创建image,或直接给image打tag
[root@client ~]# docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
registry.com:5000/test   v1                  3a792af5d5fb        26 seconds ago      203MB

3、push image

[root@client ~]# docker push registry.com:5000/test:v1
The push refers to repository [registry.com:5000/test]
884e92fd91b8: Pushed
77b174a6a187: Pushed
v1: digest: sha256:15d4af9028f9a9d478e20910f8a2c5459bbcd169a7e1ac85c243f52bc5b78dd3 size: 736

4、在registry上查看是否已经上传成功

#方式一:
[root@registry ~]# curl -XGET http://localhost:5000/v2/_catalog
{"repositories":["test"]}
[root@registry ~]#
[root@registry ~]# curl -XGET http://localhost:5000/v2/test/tags/list
{"name":"test","tags":["v1"]}
#方式二:
[root@registry ~]# python get_registry_images.py
localhost:5000/test:v1		#这个就是刚才上传的image

python code

#!/usr/bin/python

import requests
import json
import traceback

repo_ip = 'localhost'
repo_port = 5000

def getImagesNames(repo_ip,repo_port):
    docker_images = []
    try:
        url = "http://" + repo_ip + ":" +str(repo_port) + "/v2/_catalog"
        res =requests.get(url).content.strip()
        res_dic = json.loads(res)
        images_type = res_dic['repositories']
        for i in images_type:
            url2 = "http://" + repo_ip + ":" +str(repo_port) +"/v2/" + str(i) + "/tags/list"
            res2 =requests.get(url2).content.strip()
            res_dic2 = json.loads(res2)
            name = res_dic2['name']
            tags = res_dic2['tags']
            for tag in tags:
                docker_name = str(repo_ip) + ":" + str(repo_port) + "/" + name + ":" + tag
                docker_images.append(docker_name)
                print docker_name
    except:
        traceback.print_exc()
    return docker_images

a=getImagesNames(repo_ip, repo_port)

5、pull image(1.10)

[root@registry ~]# vim /etc/docker/daemon.json

{"registry-mirrors": ["http://hub-mirror.c.163.com"],
 "insecure-registries": ["registry.com:5000"]
                        }
[root@registry ~]# systemctl restart docker
[root@registry ~]# docker pull registry.com:5000/test:v1
v1: Pulling from test
ab5ef0e58194: Already exists
daed4e8ee1e6: Pull complete
Digest: sha256:15d4af9028f9a9d478e20910f8a2c5459bbcd169a7e1ac85c243f52bc5b78dd3
Status: Downloaded newer image for registry.com:5000/test:v1
registry.com:5000/test:v1
[root@registry ~]#
[root@registry ~]# docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
registry.com:5000/test   v1                  3a792af5d5fb        About an hour ago   203MB

三、Delete image(1.10)

1、查看镜像和大小(以registry.com:5000/test:v1镜像为列)

[root@registry ~]# python get_registry_images.py
localhost:5000/test:v1
[root@registry ~]# curl -X GET http://localhost:5000/v2/_catalog	#列出所有镜像仓库
{"repositories":["test"]}
[root@registry ~]# curl -X GET http://localhost:5000/v2/test/tags/list	#列出指定镜像的所有标签
{"name":"test","tags":["v1"]}
[root@registry ~]# du -sh /data/lib/registry/
73M	/data/lib/registry/		#记住删除前大小

2、删除registry中的镜像
2.1 、先找到该镜像的digest sha256值

[root@registry ~]# curl -v --silent -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -X GET  http://localhost:5000/v2/test/manifests/v1 2>&1 | grep Docker-Content-Digest | awk '{print ($3)}'
sha256:15d4af9028f9a9d478e20910f8a2c5459bbcd169a7e1ac85c243f52bc5b78dd3

2.2 、根据digest sha256值删除镜像(这里只是删除了元数据)

[root@registry ~]# curl -v --silent -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -X DELETE http://localhost:5000/v2/test/manifests/sha256:15d4af9028f9a9d478e20910f8a2c5459bbcd169a7e1ac85c243f52bc5b78dd3
* About to connect() to localhost port 5000 (#0)
*   Trying ::1...
* Connected to localhost (::1) port 5000 (#0)
> DELETE /v2/test/manifests/sha256:15d4af9028f9a9d478e20910f8a2c5459bbcd169a7e1ac85c243f52bc5b78dd3 HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:5000
> Accept: application/vnd.docker.distribution.manifest.v2+json
>
< HTTP/1.1 202 Accepted
< Docker-Distribution-Api-Version: registry/2.0
< Date: Wed, 04 Dec 2019 03:01:09 GMT
< Content-Length: 0
< Content-Type: text/plain; charset=utf-8
<
* Connection #0 to host localhost left intact

2.3 、再次查看镜像

[root@registry ~]# curl -X GET http://localhost:5000/v2/test/tags/list
{"name":"test","tags":null}		# 已经没有tag
[root@registry ~]# du -sh /data/lib/registry/
73M	/data/lib/registry/		# 大小没变

2.4 、垃圾回收

[root@registry ~]# registry garbage-collect /etc/docker-distribution/registry/config.yml
test

0 blobs marked, 5 blobs eligible for deletion
blob eligible for deletion: sha256:15d4af9028f9a9d478e20910f8a2c5459bbcd169a7e1ac85c243f52bc5b78dd3
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/15/15d4af9028f9a9d478e20910f8a2c5459bbcd169a7e1ac85c243f52bc5b78dd3  go.version=go1.9.4 instance.id=c933ebeb-7186-406c-9c78-654d1dd0b242
blob eligible for deletion: sha256:3a792af5d5fbf2d7c286628ae776929d1f05e9a5e6cc3f350795784ec21a4339
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/3a/3a792af5d5fbf2d7c286628ae776929d1f05e9a5e6cc3f350795784ec21a4339  go.version=go1.9.4 instance.id=c933ebeb-7186-406c-9c78-654d1dd0b242
blob eligible for deletion: sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/a3/a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4  go.version=go1.9.4 instance.id=c933ebeb-7186-406c-9c78-654d1dd0b242
blob eligible for deletion: sha256:ab5ef0e5819490abe86106fd9f4381123e37a03e80e650be39f7938d30ecb530
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/ab/ab5ef0e5819490abe86106fd9f4381123e37a03e80e650be39f7938d30ecb530  go.version=go1.9.4 instance.id=c933ebeb-7186-406c-9c78-654d1dd0b242
blob eligible for deletion: sha256:daed4e8ee1e697f2d837bac3298fb76c4cc68f27af010847bad252d6ab4a6fd4
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/da/daed4e8ee1e697f2d837bac3298fb76c4cc68f27af010847bad252d6ab4a6fd4  go.version=go1.9.4 instance.id=c933ebeb-7186-406c-9c78-654d1dd0b242
[root@registry ~]#
[root@registry ~]# du -sh /data/lib/registry/
20K	/data/lib/registry/		# 大小已改变

四、Upload test again

1、client push(1.11)

[root@client ~]# docker push registry.com:5000/test:v1
The push refers to repository [registry.com:5000/test]
884e92fd91b8: Pushed
77b174a6a187: Pushed
v1: digest: sha256:15d4af9028f9a9d478e20910f8a2c5459bbcd169a7e1ac85c243f52bc5b78dd3 size: 736

2、registry check(1.10)

[root@registry ~]# curl -X GET http://localhost:5000/v2/test/tags/list
{"name":"test","tags":["v1"]}
[root@registry ~]#
[root@registry ~]# docker pull registry.com:5000/test:v1
v1: Pulling from test
ab5ef0e58194: Already exists
daed4e8ee1e6: Pull complete
Digest: sha256:15d4af9028f9a9d478e20910f8a2c5459bbcd169a7e1ac85c243f52bc5b78dd3
Status: Downloaded newer image for registry.com:5000/test:v1
registry.com:5000/test:v1
[root@registry ~]#
[root@registry ~]# docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
registry.com:5000/test   v1                  3a792af5d5fb        17 hours ago        203MB

Complete!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值