Docker-swarm 自动从私有 docker-registry 中拉取丢失的镜像

Docker-swarm automatically pulling missing image from private docker-registry

我们有一个 docker-swarm 集群。管理器节点持有我们私有 docker 注册表的凭据。当我们在其他节点上启动新容器时,可能需要从私有 docker 注册表中提取图像。

有没有办法告诉 crate 容器命令拉取镜像(如果节点上尚不存在)并为其提供凭据?

例如:我们想从 swarm 管理器(具有私有 docker 注册表的凭据)调用以下命令:

docker create -e contraint:server==app docker.exampleRegistry.com/path/image:1.0.0

这应该会拉取镜像并在另一个节点(标记为 app)上启动一个容器。 docker 注册表 docker.exampleRegistry.com 是私有的。但是我们得到:

Error response from daemon: unauthorized: authentication required

知道了! docker cli 可能无法做到这一点(我们的 java driver 也不能)但是 docker 远程 API 可以使用 X-Registry-Auth header 提供凭据:

curl -v -X POST -H "Content-Type: application/json"\
-H "X-Registry-Auth: <base64ecoded({"username": "string", "password": "string", "email": "string", "serveraddress" : "string", "auth": ""})>"\
-d '{"Image": "docker.exampleRegistry.com/path/image:1.0.0", "Env": ["contraint:server==app"]}'\
https://localhost:2376/containers/create?name=container \
--key path_to_key \
--cacert path_to_ca

https://github.com/docker/docker/blob/master/docs/reference/api/docker_remote_api.md#authentication