栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

本地docker registry删image

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

本地docker registry删image

在 [1] 搭完本地 docker registry、push 了一些 images 之后,想删一些没用的 images,参考 [2,3],主要步骤是:

  1. 用 curl 127.0.0.1:5000/v2/_catalog 查看有哪些 repositories,输出形如:
    {"repositories":["keras"]}
    
  2. 根据第一步的 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"]}
    
  3. : 就是完整的 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: 那一行。
  4. 根据 repository 和上一步的 digest,用 curl -XDELETe 127.0.0.1:5000/v2/keras/manifests/sha256:c5d09c09df0b7c4ceb2e8af6a5c96acd5dac9cd0d45d0e644a4df9d8483ddb3f 删除。
  5. 用第二步验证一下,已经删掉了。但是从 [2] 知,这样只是删了元信息(记录),还没真正删 registry 中的 image。需要进 registry 进行垃圾清理。
  6. docker exec 连进 registry 容器,注意要用 sh,里面没有 bash。然后执行:
    cd /var/lib/registry/  # 去存 image 的路径
    du -sch  # 删之前看下大小
    registry garbage-collect /etc/docker/registry/config.yml  # 垃圾清理
    du -sch  # 删之后再看下大小 -> 确实变小了
    
helper script get_images.sh

把前 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
  1. 从本地docker image构建singularity image
  2. docker私有仓库删除image
  3. 私有 Docker Registry 删除镜像
  4. Bash regex capture group
  5. Bash: Using BASH_REMATCH to pull capture groups from a regex
  6. Regular Expressions/POSIX Basic Regular Expressions
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/849993.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号