如何从 Docker Swarm 中的 Google Container Registry 拉取镜像

How to pull images from Google Container Registry inside Docker Swarm

我有一个配置如下的 2 节点 swarm 集群

我已经配置 Google Container Registry 来推送和拉取图像

在配置了 docker swarm 的 host/local 机器上,我可以使用以下命令和 push/pull 图像

轻松登录到 google 容器注册表
$ gcloud auth print-access-token | sudo docker login -u oauth2accesstoken --password-stdin https://gcr.io
WARNING: Could not setup log file in /home/arush/.config/gcloud/logs, (PermissionError: [Errno 13] Permission denied: '/home/arush/.config/gcloud/logs/2021.03.09/15.17.13.106143.log')
WARNING! Your password will be stored unencrypted in /home/arush/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

要登录,我使用提到的访问令牌方法here

现在,如果我尝试使用以下命令在管理器节点上的 swarm 内部创建服务,我会收到类似图像未找到的消息

$ docker service create --name app1 --with-registry-auth -p 5003:5000 app1

如何从 swarm 集群中登录 google 容器注册表拉取镜像并创建服务

我看到您正在尝试在您的 Swarm 集群中创建一个服务,并且您想要从 Google Container Registry.

拉取您的镜像

请按照以下步骤操作:

  1. 连接到您的 Container Registry:(您可以使用 Access Token or Json File 来执行此操作)。
$ docker login -u oauth2accesstoken --password-stdin https://HOSTNAME

$ docker login -u _json_key -p "$(cat keyfile.json)" https://HOSTNAME

其中 HOSTNAME 是 gcr.io、us.gcr.io、eu.gcr.io 或 asia.gcr.io

2) Pull 来自 Container Registry 的镜像

$ docker pull HOSTNAME/PROJECT-ID/IMAGE:TAG

3) 使用拉取的镜像从您的 Swarm 节点创建一个服务

$ docker service create --with-registry-auth --name app1 HOSTNAME/PROJECT-ID/IMAGE:TAG

如果您从 Manager 节点 创建服务,请注意,Manager 节点不会自动与其他节点共享本地图像。

因此您将需要使用可从集群的所有节点访问的注册表。但是您不必使用外部公共远程存储库,您可以使用私有注册表映像在 swarm 上创建一个所有节点都可以访问的服务,如下所示:

docker service create --name registry --publish 5000:5000 registry:2

这样,所有节点都能够连接到“localhost:5000”上的注册表,并将它们需要的映像拉到 运行 您服务的容器中。

然后从该图像创建服务:

docker service create --name myservice localhost:5000/myimage