【问题标题】:How can I list and delete GCP AppEngine images from the command line?如何从命令行列出和删除 GCP AppEngine 图像?
【发布时间】:2019-04-16 22:37:51
【问题描述】:

我想从 Google Cloud Platform 中删除未使用的旧 AppEngine 图像,这样我就无需为存储它们付费。

我可以从这个 URL 手动列出和删除为我的 GCP AppEngine 项目创建的图像: https://console.cloud.google.com/gcr/images/GOOGLE_CLOUD_PROJECT_ID

(显然,将 GOOGLE_CLOUD_PROJECT_ID 替换为适当的 GCP 项目 ID。)

有没有办法从命令行列出它们?例如通过gcloud?

这不符合我的预期:

$ gcloud compute images list --no-standard-images
Listed 0 items.

这也不行:

$ gcloud container images list 
Listed 0 items.
Only listing images in gcr.io/GOOGLE_CLOUD_PROJECT_ID. Use --repository to list images in other repositories.

因为https://console.cloud.google.com/gcr/images/GOOGLE_CLOUD_PROJECT_ID/US/appengine下的每张图片都在一个单独的目录中,我必须先点击进去选择图片,然后点击删除按钮,然后返回到 appengine 目录并再次为任何其他图像启动该过程。

【问题讨论】:

标签: google-app-engine google-cloud-platform gcloud google-container-registry


【解决方案1】:

列出图片

感谢 LundinCast 的回答,我使用 us.gcr.io 作为主机名,并且能够做到这一点:

$ gcloud container images list --repository=us.gcr.io/GCP_PROJECT_ID
NAME
us.gcr.io/GCP_PROJECT_ID/appengine

显然 AppEngine 使用嵌套存储库:

$ gcloud container images list --repository=us.gcr.io/GCP_PROJECT_ID/appengine
NAME
us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425
us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726
us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t162317
us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181113t132223
us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181113t150454
us.gcr.io/GCP_PROJECT_ID/appengine/default.20181108t184629
us.gcr.io/GCP_PROJECT_ID/appengine/default.20181108t205831
us.gcr.io/GCP_PROJECT_ID/appengine/DEF.20181113t163644

这并没有像我预期的那样工作:

$ gcloud container images list --repository=us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425
Listed 0 items.

相反,我必须这样做:

$ gcloud container images list-tags us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425 
DIGEST        TAGS    TIMESTAMP
c8e3797d36b2  latest  2018-11-10T12:57:58

删除图片

根据documentation for deleting images,可以删除...

  • 由摘要识别的图像
  • 由标签标识的图像,并且它有多个标签

通过(隐式)标签删除图片

当我试图删除单个版本/标签时,我看到了一个提示:

$ gcloud container images delete us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425 
WARNING: Implicit ":latest" tag specified: us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425
Digests:
- us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425@sha256:c8e3797d36b27599c91e231afd5521e6a1283319fce5a0fd978b7cdcdb1d4181
  Associated tags:
 - latest
Tags:
- us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425:latest
This operation will delete the tags and images identified by the 
digests above.

Do you want to continue (Y/n)?  n

ERROR: (gcloud.container.images.delete) Aborted by user.

这对于编写可以自动删除旧图像的脚本或程序来说有点问题(尽管我可以将yes 命令通过管道传递给gcloud container images delete IMAGE 命令);幸运的是,可以使用 --quiet 参数:

$ gcloud container images delete us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425 --quiet
WARNING: Implicit ":latest" tag specified: us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425
Digests:
- us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425@sha256:c8e3797d36b27599c91e231afd5521e6a1283319fce5a0fd978b7cdcdb1d4181
  Associated tags:
 - latest
Tags:
- us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425:latest
Deleted [us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425:latest].
Deleted [us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t125425@sha256:c8e3797d36b27599c91e231afd5521e6a1283319fce5a0fd978b7cdcdb1d4181].

所以,这行得通。

按摘要删除图片

在另一个图像上再试一次,用删除图像的摘要方法:

$ gcloud container images list-tags us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726
DIGEST        TAGS    TIMESTAMP
4d860e73f85f  latest  2018-11-10T15:50:55

惊喜!您不能只使用列出的摘要:

$ gcloud container images delete us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726@4d860e73f85f
ERROR: (gcloud.container.images.delete) [us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726@4d860e73f85f] digest must be of the form "sha256:<digest>".

这有点奇怪(只有一个标签,“最新”,甚至没有显示在错误中):

$ gcloud container images delete us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726@sha256:4d860e73f85f
ERROR: Tags:
ERROR: (gcloud.container.images.delete) This operation will implicitly delete the tags listed above. Please manually remove with the `untag` command or re-run with --force-delete-tags to confirm.

好的,让我们使用--force-delete-tags

$ gcloud container images delete us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726@sha256:4d860e73f85f --force-delete-tags
Digests:
- us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726@sha256:4d860e73f85fdddb485e85dc867653e3e6095a5225f4f670200c98ede10fb542
  Associated tags:
 - latest
This operation will delete the tags and images identified by the digests above.

Do you want to continue (Y/n)?  n

ERROR: (gcloud.container.images.delete) Aborted by user.

和以前一样,让我们​​使用--quiet 来防止显示提示:

$ gcloud container images delete us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726@sha256:4d860e73f85f --force-delete-tags --quiet
Digests:
- us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726@sha256:4d860e73f85fdddb485e85dc867653e3e6095a5225f4f670200c98ede10fb542
  Associated tags:
 - latest
Deleted [us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726:latest].
Deleted [us.gcr.io/GCP_PROJECT_ID/appengine/ABC.20181110t154726@sha256:4d860e73f85fdddb485e85dc867653e3e6095a5225f4f670200c98ede10fb542].

因此,按摘要删除需要更详细的命令行。

【讨论】:

    【解决方案2】:

    您提供的开发者控制台链接指向 Container Registry,您的容器图像存储在其中,包括在 App Engine Flexible 部署期间创建的容器图像。

    您可以使用以下 gcloud 命令按位置列出这些图像:

    gcloud container images list --repository=[HOSTNAME]/[PROJECT-ID]
    

    其中 [HOSTNAME] 是以下四个选项之一:gcr.ious.gcr.ioeu.gcr.ioasia.gcr.io

    然后您可以使用以下命令删除图像:

    gcloud container images delete [HOSTNAME]/[PROJECT-ID]/[IMAGE]@[IMAGE_DIGEST]
    

    您可以在在线documentation 中找到有关它的详细信息。

    【讨论】:

    • 谢谢,我错过了主机名的其他选项。就我而言,我需要使用 us.gcr.io。还有一点我必须做的;我会把它放在一个单独的答案中。
    猜你喜欢
    • 1970-01-01
    • 2021-01-04
    • 2017-10-27
    • 2022-11-07
    • 2017-04-15
    • 1970-01-01
    • 2023-04-09
    • 2021-03-26
    • 1970-01-01
    相关资源
    最近更新 更多