registry删除镜像的两种办法
问题描述:
由于某些原因我们需要删除docker registry中的镜像,对于新手来说这个操作可能无从下手,下面我提供两种方式解决这个问题(推荐第一种):
一、调用registry的接口进行删除
步骤如下(请严格按照我的步骤操作,否则可能会失败):
①进入registry容器:
docker exec -it registry /bin/sh
②编辑容器内/etc/docker/registry/config.yml配置文件增加如下配置:
storage:
delete:
enabled: true
修改后效果如下:
③重启registry容器:
docker restart registry
④查询镜像tag,接口请求格式为:
curl <镜像仓库地址>/v2/<镜像名>/tags/list
示例如下:
curl http://172.15.110.35:5555/v2/openresty/tags/list
响应如下:
{"name":"openresty","tags":["1.15.8.1-20200728","test"]}
⑤查询对应tag镜像的digest_hash,接口请求格式为:
curl --header "Accept: application/vnd.docker.distribution.manifest.v2+json" -I -X GET <镜像仓库>/v2/<镜像名称>/manifests/<镜像tag>
示例如下:
curl --header "Accept: application/vnd.docker.distribution.manifest.v2+json" -I -X GET http://172.15.110.35:5555/v2/openresty/manifests/test
响应如下:
HTTP/1.1 200 OK
Content-Length: 952
Content-Type: application/vnd.docker.distribution.manifest.v2+json
Docker-Content-Digest: sha256:815386ebbe9a3490f38785ab11bda34ec8dacf4634af77b8912832d4f85dca04
Docker-Distribution-Api-Version: registry/2.0
Etag: "sha256:815386ebbe9a3490f38785ab11bda34ec8dacf4634af77b8912832d4f85dca04"
X-Content-Type-Options: nosniff
Date: Wed, 08 Dec 2021 03:35:33 GMT
记住Docker-Content-Digest:后面的值
⑥调用接口删除镜像,接口请求格式为:
curl -I -X DELETE <镜像地址>/v2/<镜像名称>/manifests/<Docker-Content-Digest>
请求示例:
curl -I -X DELETE http://172.15.110.35:5555/v2/openresty/manifests/sha256:815386ebbe9a3490f38785ab11bda34ec8dacf4634af77b8912832d4f85dca04
响应示例为:
HTTP/1.1 202 Accepted
Docker-Distribution-Api-Version: registry/2.0
X-Content-Type-Options: nosniff
Date: Wed, 08 Dec 2021 02:59:01 GMT
Content-Length: 0
⑦执行垃圾回收,清理镜像文件,命令格式为:
docker exec -it <镜像仓库容器ID/名称> /bin/registry garbage-collect <镜像仓库配置文件>
示例为:
docker exec -it registry /bin/registry garbage-collect /etc/docker/registry/config.yml
二、直接删除镜像的repositories(不建议新手使用)
①打开镜像的存储目录,删除镜像文件夹,格式为:
docker exec registry /bin/sh rm -rf /var/lib/registry/docker/registry/v2/repositories/<镜像名称>
示例为:
docker exec registry /bin/sh rm -rf /var/lib/registry/docker/registry/v2/repositories/openresty
②执行垃圾回收,清理镜像文件,命令格式为:
docker exec <镜像仓库容器ID/名称> /bin/registry garbage-collect <镜像仓库配置文件>
示例为:
docker exec registry /bin/registry garbage-collect /etc/docker/registry/config.yml