在 [1] 搭完本地 docker registry、push 了一些 images 之后,想删一些没用的 images,参考 [2,3],主要步骤是:
- 用 curl 127.0.0.1:5000/v2/_catalog 查看有哪些 repositories,输出形如:
{"repositories":["keras"]} - 根据第一步的 repository(本例即 keras),用 curl 127.0.0.1:5000/v2/keras/tags/list 查看此 repository 下有哪些 tags,输出形如:
{"name":"keras","tags":["py36-tf1.12-gpu","2.1.4-py3-tf-gpu"]} : 就是完整的 docker image 名字。比如想删掉 keras:2.1.4-py3-tf-gpu,就用 curl --header "Accept: application/vnd.docker.distribution.manifest.v2+json" -I -XGET http://127.0.0.1:5000/v2/keras/manifests/2.1.4-py3-tf-gpu 查其 digest,输出形如: HTTP/1.1 200 OK Content-Length: 8722 Content-Type: application/vnd.docker.distribution.manifest.v2+json Docker-Content-Digest: sha256:c5d09c09df0b7c4ceb2e8af6a5c96acd5dac9cd0d45d0e644a4df9d8483ddb3f Docker-Distribution-Api-Version: registry/2.0 Etag: "sha256:c5d09c09df0b7c4ceb2e8af6a5c96acd5dac9cd0d45d0e644a4df9d8483ddb3f" X-Content-Type-Options: nosniff Date: Mon, 02 May 2022 04:22:01 GMT
取其中 Docker-Content-Digest: 那一行。- 根据 repository 和上一步的 digest,用 curl -XDELETe 127.0.0.1:5000/v2/keras/manifests/sha256:c5d09c09df0b7c4ceb2e8af6a5c96acd5dac9cd0d45d0e644a4df9d8483ddb3f 删除。
- 用第二步验证一下,已经删掉了。但是从 [2] 知,这样只是删了元信息(记录),还没真正删 registry 中的 image。需要进 registry 进行垃圾清理。
- docker exec 连进 registry 容器,注意要用 sh,里面没有 bash。然后执行:
cd /var/lib/registry/ # 去存 image 的路径 du -sch # 删之前看下大小 registry garbage-collect /etc/docker/registry/config.yml # 垃圾清理 du -sch # 删之后再看下大小 -> 确实变小了
把前 3 步查 repositories、tags、digest 写成一个 shell,方便查看。
- 正则进行多次匹配参考 [4,5,6]
- 用法:bash get_images.sh
#!/bin/bash
repo_list=()
tag_list=()
# 匹配 repositories 和 tags 是一样的
# 用 $3 的值指明匹配 repo 还是 tag
# $3 = 1 就操作 repo_list,= 2 就操作 tag_list
global_rematch()
{
local s=$1 regex=$2
if [ $3 -eq 1 ]; then
repo_list=()
elif [ $3 -eq 2 ]; then
tag_list=()
fi
while [[ $s =~ $regex ]]; do
if [ $3 -eq 1 ]; then
repo_list=(${repo_list[@]} ${BASH_REMATCH[1]})
elif [ $3 -eq 2 ]; then
tag_list=(${tag_list[@]} ${BASH_REMATCH[1]})
fi
s=${s#*"${BASH_REMATCH[1]}"}
done
}
# {"repositories":["keras"]}
# {"name":"keras","tags":["py36-tf1.12-gpu","2.1.4-py3-tf-gpu"]}
PAT='[,[]"([A-Za-z0-9.-]+)"[],]'
global_rematch `curl 127.0.0.1:5000/v2/_catalog 2>/dev/null` $PAT 1
if [ 0 -eq ${#repo_list[@]} ]; then
# echo 没有 repository
exit
fi
# 对每一个 repository 查其 tags 和对应 digest
for _repo in ${repo_list[@]}; do
echo $_repo
global_rematch `curl 127.0.0.1:5000/v2/${_repo}/tags/list 2>/dev/null` $PAT 2
for _tag in ${tag_list[@]}; do
_digest=$(curl --header "Accept: application/vnd.docker.distribution.manifest.v2+json"
-I -XGET http://127.0.0.1:5000/v2/$_repo/manifests/$_tag 2>/dev/null |
awk '$1 ~/Docker-Content-Digest:/ {print $2}')
echo |- $_tag
echo | |- $_digest
done
done
del_image.sh
- 拿到 repo 和 digest 之后,删除脚本如下
- 用法:sh del_image.sh
#!/bin/bash
if [ $# -ne 2 ]; then
echo USAGE: sh del_image.sh
echo got #param: $#
echo For and its corresponding
echo run `bash get_images.sh` "for information"
exit
fi
curl -XDELETE 127.0.0.1:5000/v2/$1/manifests/$2
References
- 从本地docker image构建singularity image
- docker私有仓库删除image
- 私有 Docker Registry 删除镜像
- Bash regex capture group
- Bash: Using BASH_REMATCH to pull capture groups from a regex
- Regular Expressions/POSIX Basic Regular Expressions



