【问题标题】:Tag image in Google Container Registry with Docker API for Python使用 Docker API for Python 在 Google Container Registry 中标记图像
【发布时间】:2021-10-21 10:59:08
【问题描述】:

我在 Google Cloud 中有一个项目,我想在 Container Registry 中使用 new_tag 标记图像。我写了代码:

import docker

cli = docker.from_env()
img = cli.images.get('my_image:latest')
tagged = img.tag('my_image:new_tag')
print('image tagged: ' + str(tagged))

如何为 docker.from_env 设置参数,例如主机 (gcr.io)、项目 (my_project) 和使用谷歌云服务帐户的身份验证?

【问题讨论】:

  • 你提供的python脚本试过了吗?此外,您是否只想在 Google Container Registry 中为图像添加新标签?为此,您可以使用this document 中提到的gcloud 命令。
  • @Prabir 该脚本在本地运行良好,但我需要连接到远程存储库 (gcr.io)。是的,我只想标记现有图像。而且我知道如何使用 gcloud 命令执行此操作,但我想使用前端制作简单的应用程序,以便能够在浏览器中使用新标签标记所需的图像

标签: python docker google-container-registry


【解决方案1】:

编写一个 python 程序,要求用户输入 10 个 integrrs 。然后它执行以下操作: - 打印列表中的最大值、最小值 - 打印列表中最大的奇数。如果输入中没有奇数,打印“没有奇数 数字” 例如 : 输入结果 10 120 20 5 30 101 40 5 6 7 90 101 120

2 100 4 2 6 没有奇数 8 10 20 30 40 50 80 100

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

由于 Google Container Registry 是一个远程注册表,因此除了使用 here 提到的 gcloud 之外,无法从本地系统直接添加标签。

有一种方法可以通过运行 python 脚本来添加新标签。为此,您必须 -

  1. 从 Google Container Registry 中拉取镜像
  2. 在本地添加新标签
  3. 将新标记的图像推送到 Google Container Registry

以下 python 脚本非常适合此任务:

import docker
client = docker.from_env()
base_url = "gcr.io/project-id/demo-image"
#existing tag of the image in gcr
from_url=":tag1"
#desired tag to be added
to_tag = ":tag2"
#pull the image from container registry to local system
client.images.pull(base_url+from_url)
#get the image from local system and add a new tag to it
client.images.get(base_url+from_url).tag(base_url+to_tag)
#push the newly tagged image to container registry
client.images.push(base_url+to_tag)
#remove the pulled image from local system
client.images.remove(base_url+to_tag)
#remove the newly tagged image from local system
client.images.remove(base_url+from_url)

【讨论】:

    猜你喜欢
    • 2017-09-27
    • 2018-10-31
    • 2021-06-07
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 2021-11-28
    相关资源
    最近更新 更多