【问题标题】:Docker Swarm- How to deploy a local docker image from manager to all worker nodesDocker Swarm-如何将本地 Docker 映像从管理器部署到所有工作节点
【发布时间】:2018-01-10 06:54:53
【问题描述】:

我有 3 台 linux 机器,其中一台机器为 manager,另外两台机器为 worker1 和 worker2。所以假设我有 docker 图像,并且我已经在 manager 上测试过它,它工作正常。现在我想在所有节点上部署相同的。为此,我首先将图像推送到 docker hub,然后当它在 docker hub 上可用时,我运行命令

sudo docker service create --name <name> --mode global <docker-name/image-name>

然后在所有工作节点上开始部署,一段时间后,工作人员正在运行已部署的 docker 映像。现在我想知道是否可以在工作节点上部署图像而无需将该图像推送到 docker hub 上。因此,我在管理器节点上有一个本地可用的 docker 映像,我只想将该映像部署在工作节点上。我怎样才能做到这一点?

接下来我想知道,当我启动我的 docker 镜像时,我使用-v 选项来提供我的挂载目录的路径。在部署过程中如何使用这个-v 来选择?

【问题讨论】:

标签: docker docker-swarm swarm


【解决方案1】:

现在我想知道是否可以在工作节点上部署映像而不将该映像推送到 docker hub 上。因此,我在管理器节点上有一个本地可用的 docker 映像,我只想将该映像部署在工作节点上。我怎样才能做到这一点?

来自@BMitch 的引用:

管理器节点不会从自身共享本地图像。你需要启动一个注册服务器。

#first create a user, updating $user for your environment:
if [ ! -d "auth" ]; then
  mkdir -p auth
fi
chmod 666 auth/htpasswd
docker run --rm -it \
  -v `pwd`/auth:/auth \
  --entrypoint htpasswd registry:2 -B /auth/htpasswd $user
chmod 444 auth/htpasswd

# then spin up the registry service listening on port 5000
docker run -d -p 5000:5000 --restart=always --name registry \
  -v `pwd`/auth/htpasswd:/auth/htpasswd:ro \
  -v `pwd`/registry:/var/lib/registry \
  -e "REGISTRY_AUTH=htpasswd" \
  -e "REGISTRY_AUTH_HTPASSWD_REALM=Local Registry" \
  -e "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd" \
  -e "REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY=/var/lib/registry" \
  registry:2

# then push your image
docker login localhost:5000
docker tag my-customized-image localhost:5000/my-customized-image
docker push localhost:5000/my-customized-image

# then spin up the service with the new image name
# replace registryhost with ip/hostname of your registry Docker host
docker service create --name custom --network my-network \
  --constraint node.labels.myconstraint==true --with-registry-auth \
  registryhost:5000/my-customized-image

我觉得这和this很像

【讨论】:

  • 在最后一个命令docker service create... 中--network my-network 和--constraint 意味着我必须在这些标志中添加什么。你能解释一下这个命令吗?谢谢
  • --network 是您想要使用的 docker 网络类型,请使用 docker network ls 检查您的可用网络,了解更多关于 docker 网络 docs.docker.com/engine/reference/commandline/network_create 的信息 ...截至 --constraint 是一种节点过滤以确保两个容器不在同一个节点上。docs.docker.com/swarm/scheduler/filter
  • 谢谢。正如我所提到的,我正在使用树莓派,因此运行命令docker run -d -p 5000:5000 --restart=always --name registry... 给了我错误,因为树莓派不支持此操作。所以我使用了this。这工作正常。当我在管理节点上执行curl localhost:5000/v2/_catalog 时,我什至将我的图像推送到注册表,我得到了图像列表,但是当我在工作节点上执行相同操作时,它给了我错误Failed to connect to localhost:5000。有什么我遗漏的。因此我无法部署它
  • 等等,你之前有没有提到过 raspberry pi,?我以为你只是在说 3 台 linux 机器..
  • 对此我感到非常抱歉。如果您对此有所了解,请解释一下。?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多