环境准备
系统:



cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core)



 

主机两台,分别是docker私有库服务器(IP 192.168.121.121)和用户开发机(IP 192.168.121.122),开发机从私有库服务器拉取镜像。

 


 

1、配置软件源并安装安装docker
两台主机安装docker



yum install docker-ce



 

2、搭建私有镜像仓库
登陆私有库服务器

创建docker管理账户并设置密码



useradd dkuser
passwd dkuser



把账户加入docker组,这一步是为了可以使用普通账户管理docker,而不用使用sudo命令



usermod -G docker dkuser



为账户配置sudo权限



visudo



添加下面一行
dkuser ALL=(ALL) NOPASSWD:ALL

 

切换账户



su dkuser



启动docker服务



sudo systemctl start docker



 

可以通过官方提供的registry镜像来搭建本地的私有仓库,所以拉取官方镜像



docker pull registry



 

创建存放镜像和docker配置文件的目录



sudo mkdir /data/docker



 

创建配置文件,storage配置中的delete=true配置项,是为了允许删除镜像。默认的镜像是没有这个参数



sudo vi /data/docker/config.yml




docker建立私有仓库 docker私有仓库删除镜像_docker

docker建立私有仓库 docker私有仓库删除镜像_json_02

1 version: 0.1
 2 
 3 log:
 4 fields:
 5 service: registry
 6 storage:
 7 delete:
 8 enabled: true
 9 cache:
10 blobdescriptor: inmemory
11 filesystem:
12 rootdirectory: /var/lib/registry
13 http:
14 addr: :5000
15 headers:
16 X-Content-Type-Options: [nosniff]
17 health:
18 storagedriver:
19 enabled: true
20 interval: 10s
21 threshold: 3


View Code


 

运行registry容器



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



说明:
-d -p 5000:5000 端口映射
-v /data/docker/registry:/var/lib/registry 默认情况下,会将仓库存放于容器内的/var/lib/registry目录下,指定本地目录挂载到容器
--restart always 在容器退出时总是重启容器,主要应用在生产环境
--name registry 指定容器的名称

查看容器



docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
071105c54db3 registry "/entrypoint.sh /etc…" 3 minutes ago Up About a minute 0.0.0.0:5000->5000/tcp registry



 

3、把本地镜像上传到私有仓库
在私有库服务器上

这里以busybox镜像为例,因为比较小

首先把镜像下载到本地



docker pull busybox



 

为镜像打标签



docker tag busybox 192.168.121.121:5000/busybox



 

查看镜像



docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.121.121:5000/busybox latest db8ee88ad75f 7 hours ago 1.22MB
busybox latest db8ee88ad75f 7 hours ago 1.22MB



 

编辑配置文件,添加配置是为了能够在本地上传镜像。



sudo vi /usr/lib/systemd/system/docker.service



在 ExecStart=/usr/bin/dockerd 后边添加

--insecure-registry 192.168.121.121:5000

 

重启docker服务



sudo systemctl daemon-reload
sudo systemctl restart docker



 

推送镜像到私有库



docker push 192.168.121.121:5000/busybox
The push refers to repository [192.168.121.121:5000/busybox]
0d315111b484: Pushed 
latest: digest: sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649 size: 527



 

报错解决:



docker push 192.168.121.121:5000/busybox
The push refers to repository [192.168.121.121:5000/busybox]
Get https://192.168.121.121:5000/v2/: http: server gave HTTP response to HTTPS client



 

因为Docker从1.3.X之后,与docker registry交互默认使用的是https,然而此处搭建的私有仓库只提供http服务,所以当与私有仓库交互时就会报上面的错误。为了解决这个问题需要在启动docker server时增加启动参数为默认使用http访问



sudo vi /usr/lib/systemd/system/docker.service



ExecStart=/usr/bin/dockerd 后边添加

--insecure-registry 192.168.121.121:5000

 

4、开发机从私有库载入镜像

同样的也需要添加配置



vi /usr/lib/systemd/system/docker.service



ExecStart=/usr/bin/dockerd 后边添加

--insecure-registry 192.168.121.121:5000

 

启动docker服务



systemctl start docker



 

从私有库载入镜像



docker pull 192.168.121.121:5000/busybox
Using default tag: latest
latest: Pulling from busybox
ee153a04d683: Pull complete 
Digest: sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649
Status: Downloaded newer image for 192.168.121.121:5000/busybox:latest



 

查看镜像



docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.121.121:5000/busybox latest db8ee88ad75f 7 hours ago 1.22MB



 

5、删除镜像
在私有库服务器上

相应的参考命令:
#查询镜像
curl <仓库地址>/v2/_catalog

#查询镜像tag(版本)
curl <仓库地址>/v2/<镜像名>/tags/list

#查询镜像digest_hash
curl --header "Accept:application/vnd.docker.distribution.manifest.v2+json" -I -XGET <仓库地址>/v2/<镜像名>/manifests/<tag>

#删除镜像API
curl -I -X DELETE "<仓库地址>/v2/<镜像名>/manifests/<镜像digest_hash>"

在服务器执行下面命令
查询镜像



curl -XGET http://192.168.121.121:5000/v2/_catalog
{"repositories":["busybox"]}



 

查询镜像tag



curl http://192.168.121.121:5000/v2/busybox/tags/list
{"name":"busybox","tags":["latest"]}



 

查询镜像digest_hash,删除命令里边要填写的 镜像digest_hash 就是 查询结果里边 Docker-Content-Digest: 后边的内容



curl --header "Accept:application/vnd.docker.distribution.manifest.v2+json" -I -XGET http://192.168.121.121:5000/v2/busybox/manifests/latest
HTTP/1.1 200 OK
Content-Length: 527
Content-Type: application/vnd.docker.distribution.manifest.v2+json
Docker-Content-Digest: sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649
Docker-Distribution-Api-Version: registry/2.0
Etag: "sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649"
X-Content-Type-Options: nosniff
Date: Fri, 19 Jul 2019 06:09:44 GMT



 

删除私有库镜像



curl -I -XDELETE http://192.168.121.121:5000/v2/busybox/manifests/sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649
HTTP/1.1 202 Accepted
Docker-Distribution-Api-Version: registry/2.0
X-Content-Type-Options: nosniff
Date: Fri, 19 Jul 2019 06:10:56 GMT
Content-Length: 0



 

查看镜像信息可以看到镜像的标签显示为空 null



curl http://192.168.121.121:5000/v2/busybox/tags/list
{"name":"busybox","tags":null}



 

这里虽然删除了,但是实际上硬盘地址还没有释放,是因为docker删除p_w_picpath只是删除的p_w_picpath的元数据信息。层数据并没有删除。现在进入registry中进行垃圾回收。

进入registry容器



docker exec -it 071105c54db3 /bin/sh
/ # cd /var/lib/registry/



 

查看镜像大小



/var/lib/registry # du -sch
756.0K .
756.0K total



 

执行回收命令



/var/lib/registry # registry garbage-collect /etc/docker/registry/config.yml 
busybox

0 blobs marked, 3 blobs and 0 manifests eligible for deletion
blob eligible for deletion: sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/89/895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649 go.version=go1.11.2 instance.id=7c37e39d-6ad1-4139-b9d1-592a900b0902 service=registry
blob eligible for deletion: sha256:db8ee88ad75f6bdc74663f4992a185e2722fa29573abcc1a19186cc5ec09dceb
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/db/db8ee88ad75f6bdc74663f4992a185e2722fa29573abcc1a19186cc5ec09dceb go.version=go1.11.2 instance.id=7c37e39d-6ad1-4139-b9d1-592a900b0902 service=registry
blob eligible for deletion: sha256:ee153a04d6837058642958836062f20badf39f558be3e6c7c7773ef7d8301d90
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/ee/ee153a04d6837058642958836062f20badf39f558be3e6c7c7773ef7d8301d90 go.version=go1.11.2 instance.id=7c37e39d-6ad1-4139-b9d1-592a900b0902 service=registry



 

再次查询,发现回收资源执行成功



/var/lib/registry # du -sch
0 .
0 total