【问题标题】:Docker Swarm Deploy a local DockerfileDocker Swarm 部署本地 Dockerfile
【发布时间】:2018-03-19 05:03:23
【问题描述】:

我正在尝试在本地计算机上以集群的形式部署一组服务以进行测试,并且我想在每次从管理器节点运行或部署堆栈时构建 docker 映像。

这可能是我想要实现的目标吗..

【问题讨论】:

    标签: docker docker-compose dockerfile docker-swarm


    【解决方案1】:

    在 Docker Swarm 上,您无法构建在 Docker Compose 文件中指定的映像:

    注意:在使用(版本 3)Compose 文件以 swarm 模式部署堆栈时会忽略此选项。 docker stack 命令只接受预构建的图像。 - from docker docs

    你需要用docker build创建镜像(在Dockerfile所在的文件夹上):

    docker build -t imagename --no-cache .
    

    执行此命令后,图像(名为 imagename)现在可以在本地注册表中使用。

    您可以在 Docker Compose 文件中使用此映像,如下所示:

    version: '3'
    
    services:
      example-service:
        image: imagename:latest
    

    【讨论】:

      【解决方案2】:

      您需要使用 docker build 来构建镜像。 Docker swarm 不能使用标签来识别图像。相反,它会在执行堆栈部署时记住图像的图像 ID(哈希),因为标签可能会在以后更改,但哈希永远不会改变。

      因此,您应该引用 docker image ls 所示的镜像哈希值,这样 docker swarm 就不会尝试在某个注册表中找到您的镜像。

      version: '3'
      
      services:
        example-service:
          image: imagename:97bfeeb4b649
      

      【讨论】:

      • 是的,我就是这么做的。但是,如果我的 swarm 跨越网络,则图像不可能出现在工作节点中。这是我的担忧。因此,在部署我的 swarm 之前,我必须在每个节点上构建映像。
      • 您可以将图像推送到某个注册表(我认为您应该可以免费使用 dockerhub),或者您可以简单地使用 docker save 和 docker load(如stackoverflow.com/questions/23935141/… 中所述)和usb 棒/scp 将图像复制到所有工作人员并在那里加载
      【解决方案3】:

      更新本地图像时,您将收到如下错误

      image IMAGENAME:latest could not be accessed on a registry to record
      its digest. Each node will access IMAGENAME:latest independently,
      possibly leading to different nodes running different
      versions of the image.
      

      为了克服这个问题,如下强制启动服务

      docker service update --image IMAGENAME:latest  --force  Service Name
      

      在上面的例子中它是

      docker service update --image imagename:97bfeeb4b649 --force 服务名称

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多