部署Docker distribution仓库

 环境准备:

下载docker yum文件

# wget -O /etc/yum.repos.d/aliyun.repo http://mirrors.aliyun.com/repo/Centos-7.repo 

 1、安装docker-distribution 

# yum install -y docker-distribution
# systemctl enable docker-distribution.service 
# systemctl start docker-distribution.service 
# netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp6       0      0 :::111                  :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
tcp6       0      0 :::5000                 :::*                    LISTEN     

 默认docker 客户端链接到自己搭建的镜像仓库采用https,为了采用http的方式,需要修改配置文件(2种方法):

第1种方法:

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

第2种方法:

# vim /usr/lib/systemd/system/docker.service    #增加1行启动参数
  --insecure-registry=workstation:5000 \        

# systemctl daemon-reload 
# systemctl restart docker

 2、docker 仓库常用的操作

1)列出当前所有镜像

# curl http://workstation:5000/v2/_catalog
 {"repositories":["busybox_1","nginx","wordpress"]}

2)列出当前指定镜像

# curl http://10.99.73.10:5000/v2/_catalog?n=100

3)搜索镜像

# curl http://10.99.73.10:5000/v2/wordpress/tags/list
{"name":"wordpress","tags":["latest"]}

4)确认Registry是否正常工作 

5)删除镜像

Docker仓库在2.1版本中支持了删除镜像的API,但这个删除操作只会删除镜像元数据,不会删除层数据。在2.4版本中对这一问题进行了解决,增加了一个垃圾回收命令,删除未被引用的层数据。但有一些条件限制,具体操作步骤如下:

启动仓库容器

$ docker run -d \
    -p 5000:5000 \
    --restart=always \
    --name registry \
    -v /data/:/var/lib/registry \
    -v /data/config.yml:/etc/docker/registry/config.yml \
    registry:2.4.1

这里需要说明一点,在启动仓库时,需在配置文件中的storage配置中增加delete=true配置项,允许删除镜像,本次试验采用如下配置文件:

version: 0.1
log:
  fields:
    service: registry
storage:
    delete:
        enabled: true
    cache:
        blobdescriptor: inmemory
    filesystem:
        rootdirectory: /var/lib/registry
http:
    addr: :5000
    headers:
        X-Content-Type-Options: [nosniff]
health:
  storagedriver:
    enabled: true
    interval: 10s
    threshold: 3

查看数据进行仓库容器中,通过du命令查看大小,可以看到当前仓库数据大小为339M。

# du -sh /data/docker/registry/v2/
339M /data/docker/registry/v2/

删除镜像对应的API如下: 

reference:镜像对应sha256值。

首先查看要删除镜像的sha256

# ls /data/docker/registry/v2/repositories/wordpress/_manifests/revisions/sha256/
4eefa1b7fdce1b6e6953ca18b6f49a68c541e9e07808e255c3b8cc094ff085da

进行删除操作

# curl -I -X DELETE http://workstation:5000/v2/wordpress/manifests/sha256:4eefa1b7fdce1b6e6953ca18b6f49a68c541e9e07808e255c3b8cc094ff085da
HTTP/1.1 202 Accepted
Docker-Distribution-Api-Version: registry/2.0
X-Content-Type-Options: nosniff
Date: Thu, 15 Dec 2016 06:27:19 GMT
Content-Length: 0
Content-Type: text/plain; charset=utf-8

执行垃圾回收

命令:registry garbage-collect config.yml

# docker exec -ti registry bash
root@ef45a8a624c1:/# registry garbage-collect /etc/docker/registry/config.yml

再看数据大小

# du -sh /data/docker/registry/v2/
88K /data/docker/registry/v2/

可以看到镜像数据已被删除,从339M变成了88K。

PS:尝试过直接在目录中把镜像删除,然后重启docker daemon,此镜像也会删除。

猜你喜欢

转载自www.cnblogs.com/reachos/p/8911008.html