通过 python 将 docker 图片上传到 GCR

Upload docker image to GCR through python

我正在编写 python 代码,我需要在其中构建 docker 映像并将其推送到 GCR(Google 容器注册表)。我可以使用 Docker SDK 为 Python 创建一个 docker 图像,但是我找不到推送它的方法到 gcr。我正在研究 docker_client.images.push() 方法,但我没有看到使用此方法连接到 gcr 的方法。我可以使用 docker_client.images.build() 构建 docker 映像,但无法找到任何方法将其推送到 google 容器注册表。有一些方法可以将它推送到 docker 注册表,但我需要特定于 gcr。

我已经使用 google cli 或通过 Azure DevOps 实现了这个,但是我正在尝试使用 python 应用程序来实现同样的功能。

任何 help/suggestion 不胜感激。

您查看过容器注册表 python 库吗? https://github.com/google/containerregistry

或者更好的是,您是否考虑过转行 https://github.com/google/go-containerregistry

这似乎对我有用 Docker SDK:

import docker

client  = docker.from_env()

image, logs = client.images.build(path="<path/to/your/docker-repo>")

image_dest = "gcr.io/<your-gcp-project>/<your-repo-name>"

image.tag(image_dest)

client.api.push(image_dest)

我还必须使用 gcloud auth configure-docker 添加权限。