我已将使用gcloud docker push
的容器映像推送到 Google Container Registry。两个问题:
如何从注册表中干净地删除推送的容器映像?(我知道我可以删除图像的标签并使其不再可访问。)
一个镜像带来了一堆 Docker 层。我想通过图像删除来删除所有未使用的图层。
我已将使用gcloud docker push
的容器映像推送到 Google Container Registry。两个问题:
如何从注册表中干净地删除推送的容器映像?(我知道我可以删除图像的标签并使其不再可访问。)
一个镜像带来了一堆 Docker 层。我想通过图像删除来删除所有未使用的图层。
更新:您现在可以直接从 UI 中删除单个容器图像。
截至 2015 年 11 月:目前无法从注册表中彻底删除单个容器映像。现在,基本上是全有或全无。GCR 团队正在为此努力!
原始答案:我想不出删除单个图像的简单方法。您可以使用 删除 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)运行上述命令。
一个粗略的脚本示例将运行以下帖子 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