[英]How can I cleanly remove a container image from the Google Container Registry?
我已经使用gcloud docker push
将容器镜像推送到 Google Container Registry。 两个问题:
如何从注册表中彻底删除推送的容器镜像? (我知道我可以删除图像的标签并使其无法再访问。)
图像带来了一堆 Docker 层。 我想删除所有未使用的图层并删除图像。
更新:您现在可以直接从 UI 删除单个容器图像。
截至 2015 年 11 月:目前无法从注册表中干净地删除单个容器映像。 现在,它基本上是全有或全无。 GCR 团队正在为此努力!
原始答案:我想不出一种简单的方法来删除单个图像。 您可以通过使用 gsutil rb gs://artifacts.<PROJECT-ID>.appspot.com
删除 Cloud Storage 存储gsutil rb gs://artifacts.<PROJECT-ID>.appspot.com
来删除所有图像。您还可以使用存储浏览器并尝试删除各个部分(https://console.developers.google.com/storage/browser/artifacts ..appspot.com),但您必须知道每一层的 Docker 哈希值!
这可以通过 Gcloud 完成,这意味着它可以从 CLI 或在代码管道中完成(比如在 CD 的末尾)。
正如Google 记录的那样,您可以使用以下命令收集所有未标记图像的列表:
gcloud container images list-tags [HOSTNAME]/[PROJECT-ID]/[IMAGE] --filter='-tags:*' --format="get(digest)" --limit=$BIG_NUMBER
然后删除图像:
gcloud container images delete [HOSTNAME]/[PROJECT-ID]/[IMAGE]@DIGEST --quiet
上面的命令是针对第一个命令的每个输出 (DIGEST) 运行的。
一个粗略的脚本示例将运行以下 post gcloud auth:
gcloud container images list-tags gcr.io/myProject/myApp --filter='-tags:*' --format="get(digest)" --limit=10 > tags && while read p; do gcloud container images delete "gcr.io/myProject/myApp@$p" --quiet; done < tags
Github 操作发布 CD 映像清理任务如下所示:
needs: [CI, Build_myApp]
runs-on: ubuntu-latest
steps:
- name: 'Authenticate to Gcloud'
uses: google-github-actions/setup-gcloud@master
with:
project_id: myProject
service_account_email: myServiceAccount@myProject.iam.gserviceaccount.com
service_account_key: ${{ secrets.CONTAINER_ADMIN_NP_SA }}
export_default_credentials: true
- name: 'Cleanup untagged images in nonprod'
run: gcloud container images list-tags gcr.io/myProject/myApp --filter='-tags:*' --format="get(digest)" --limit=10 > tags && while read p; do gcloud container images delete "gcr.io/myProject/myApp@$p" --quiet; done < tags
PROJECT="project"
REGISTRY="registry"
while true; do
echo "\n"
DIGEST=$(gcloud container images list-tags gcr.io/$PROJECT/$REGISTRY --format="get(digest)" --limit 1)
if [ "$DIGEST" = "" ]; then
break;
fi
gcloud container images delete gcr.io/$PROJECT/$REGISTRY@$DIGEST --force-delete-tags --quiet
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.