docker学习笔记(七):配置私有仓库

223人浏览 / 0人评论

这一章中的很多内容是我之前学过的,比如搭建仓库容器、提交镜像、下载镜像等。

相关内容可以参看:使用Docker Registry快速搭建私有镜像仓库

另外还学习到了一些以前不知道的内容。

修改docker默认仓库

通过修改docker配置文件,可以修改docker的默认仓库。

  • 修改/etc/sysconfig/docker
[root@localhost ~]# vi /etc/sysconfig/docker
  • 添加仓库:
ADD_REGISTRY='--add-registry 192.168.0.110:5000'
  • 重启docker
[root@localhost ~]# systemctl restart docker

这样就可以将docker的默认仓库设置为192.168.0.110:5000

删除仓库中镜像

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

  • 打包上传镜像
[root@master scripts]# docker tag alpine:v1 192.168.0.153:5000/fbgweb:v1
[root@master scripts]# docker push 192.168.0.153:5000/fbgweb:v1
The push refers to a repository [192.168.0.153:5000/fbgweb]
e30c8796115a: Pushed 
9922d8737f34: Pushed 
c3cc0aba53da: Pushed 
f73b42f6c905: Pushed 
4a3fe911d00f: Pushed 
187a385eda60: Pushed 
0e23c00d0c30: Pushed 
d221a7f5318b: Pushed 
7e2d3752fd4f: Pushed 
v1: digest: sha256:6a67ba482a8dd4f8143ac96b1dcffa5e45af95b8d3e37aeba72401a5afd7ab8e size: 2204
  • 查看仓库镜像 (查看仓库镜像脚本 get.py)
[root@master scripts]# python get.py                           
192.168.0.153:5000/fbgweb:v1
192.168.0.153:5000/nginx:1.7.9
192.168.0.153:5000/nginx:1.8
  • 查看数据进行仓库容器中,通过du命令查看大小
[root@master ~]# docker exec -it f70d0c79e6d546d4 sh
~ # du  -chs  /var/lib/registry/
182.4M  /var/lib/registry/
182.4M  total

删除镜像

  • 删除镜像对应的API如下:
DELETE /v2/<name>/manifests/<reference>
  • name:镜像名称

  • reference: 镜像对应sha256值

  • 发送请求,删除刚才上传的镜像

[root@master scripts]#  curl -I -X DELETE http://192.168.0.153:5000/v2/fbgweb/manifests/sha256:6a67ba482a8dd4f8143ac96b1dcffa5e45af95b8d3e37aeba72401a5afd7ab8e
HTTP/1.1 202 Accepted
Docker-Distribution-Api-Version: registry/2.0
X-Content-Type-Options: nosniff
Date: Mon, 13 Nov 2017 13:04:24 GMT
Content-Length: 0
Content-Type: text/plain; charset=utf-8
  • 查看镜像
[root@master scripts]# python get.py 
192.168.0.153:5000/nginx:1.7.9
192.168.0.153:5000/nginx:1.8
192.168.0.153:5000/nginx:latest

可以看到镜像索引已经被删除

  • 查看数据大小
[root@master ~]# docker exec -it f70d0c79e6d546d4 sh
~ # du  -chs  /var/lib/registry/
182.4M  /var/lib/registry/
182.4M  total

可以看到数据大小没有变化(只删除了元数据)

垃圾回收

  • 进行容器执行垃圾回收命令
~ # registry garbage-collect /etc/docker/registry/config.yml 
31 blobs marked, 5 blobs eligible for deletion
blob eligible for deletion: sha256:5e7cf06c8745d0985f94191c60aad8b87371c8a674162525bff0efccdb805931
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/5e/5e7cf06c8745d0985f94191c60aad8b87371c8a674162525bff0efccdb805931  go.version=go1.7.6 instance.id=c38f4c35-9914-4b77-a59f-ea584137fae0
blob eligible for deletion: sha256:6a67ba482a8dd4f8143ac96b1dcffa5e45af95b8d3e37aeba72401a5afd7ab8e
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/6a/6a67ba482a8dd4f8143ac96b1dcffa5e45af95b8d3e37aeba72401a5afd7ab8e  go.version=go1.7.6 instance.id=c38f4c35-9914-4b77-a59f-ea584137fae0
blob eligible for deletion: sha256:966e2fb5980b2a854c03551418af0b2ee2bef082dfaae075026d00fa36620960
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/96/966e2fb5980b2a854c03551418af0b2ee2bef082dfaae075026d00fa36620960  go.version=go1.7.6 instance.id=c38f4c35-9914-4b77-a59f-ea584137fae0
blob eligible for deletion: sha256:a122ad1ef7a033582abfc7bae980cc11bdcc2d19bed7d8ea5b7efc50d16456c8
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/a1/a122ad1ef7a033582abfc7bae980cc11bdcc2d19bed7d8ea5b7efc50d16456c8  go.version=go1.7.6 instance.id=c38f4c35-9914-4b77-a59f-ea584137fae0
blob eligible for deletion: sha256:b29205236f1d3eb6143e95f3a412a8f21a16a10b09c7aee28ecba3d803832285
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/b2/b29205236f1d3eb6143e95f3a412a8f21a16a10b09c7aee28ecba3d803832285  go.version=go1.7.6 instance.id=c38f4c35-9914-4b77-a59f-ea584137fae0
  • 查看数据大小
~ # du  -chs  /var/lib/registry/
159.5M  /var/lib/registry/
159.5M  total

可以看到镜像数据已被删除

参见: centos7 Docker私有仓库搭建及删除镜像

全部评论