【问题标题】:Retagging image on AWS ECR creates new image在 AWS ECR 上重新标记图像会创建新图像
【发布时间】:2022-01-04 23:22:49
【问题描述】:

我在 AWS ECR 上有一个私有 Docker 存储库。

我正在尝试使用https://docs.aws.amazon.com/AmazonECR/latest/userguide/image-retag.html 此处的说明为现有图像添加新标签

例如,假设我有一个带有标签“1.5.0”的图像,现在我想添加标签“lts”,通过使用上面的方法到标签“1.5.0”的“batch-get-image” ",然后是 "put-image" 和 "image-tag" "lts",该命令在存储库中创建一个带有标签 "lts" 的全新图像。原图“1.5.0”不受影响。

代码:

MANIFEST=$(aws ecr batch-get-image --region eu-west-1 --repository-name mynamespace/repo --image-ids imageTag='1.5.0' --query 'images[].imageManifest' --output text)
aws ecr put-image --region eu-west-1 --repository-name mynamespace/repo --image-tag lts --image-manifest "$MANIFEST"

上面的结果是两个单独的图像:

  • lts
  • 1.5.0

我想要 1 张图片:

  • 1.5.0, lts

任何想法我做错了什么?存储库设置已禁用不变性。

我宁愿避免使用 docker pulldocker tagdocker push 方法,因为这将在我的 CI 服务器(Github 操作)上以单独的工作流程运行,而 docker pull 将是一个非常浪费的命令。

【问题讨论】:

  • 你找到答案了吗?我也面临同样的问题
  • @alital - 你找到答案了吗? :)
  • 同问,找到答案了吗?
  • 这个指向put-image 文档的链接显示了“重新标记”它时正在创建的新图像,这与 AWS 描述重新标记的其他文档相反:awscli.amazonaws.com/v2/documentation/api/latest/reference/ecr/…

标签: docker amazon-ecr


【解决方案1】:

问题是清单中的空白。

--output text 将引入不会产生与原始图像相同的 sha 的空白,因此它将发布新图像。

所以你想这样使用jq

MANIFEST=$(aws ecr batch-get-image --repository-name amazonlinux --image-ids imageTag=latest --output json | jq --raw-output '.images[].imageManifest')

(如此处所述)How do I re-tag an image in ECR?

无论您使用哪种语言,都必须管理空格。对我来说,要在 ruby​​ 中管理它,对我有用的是将它解析为 json。

另外,我学到的一件事是,您可以通过指定您所期望的 sha 来保护自己免于无意中创建新图像。在下面的这段代码中,我

  • 按标签查找图片并抓取imageDigest sha,
  • 使用该 sha 获取清单
  • 将摘要 sha 清单传递给 put-image 命令

如果清单的 sha 与您提供的摘要 sha 不匹配,Amazon 将返回错误,从而允许命令失败,而不是创建您不想要的附加图像。

digest = `aws ecr batch-get-image --repository-name #{name} --image-ids=imageTag=#{tag} --output json | jq --raw-output '.images[0].imageId.imageDigest'`.strip

pre_manifest = `aws ecr batch-get-image --repository-name #{name} --image-ids=imageDigest=#{digest} --output json | jq '.images[0].imageManifest'`

manifest = JSON.parse(pre_manifest)

# run_command is a wrapper around a system call

run_command("aws ecr put-image --repository-name #{name} --image-tag latest --image-digest #{digest} --image-manifest '#{manifest}'")

【讨论】:

    猜你喜欢
    • 2020-05-17
    • 2021-10-17
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 2020-06-05
    相关资源
    最近更新 更多