简体   繁体   English

如何动态更改 docker-compose 图像字段

[英]How to dynamically change the docker-compose image field

I have a docker-compose.yml something like bellow:我有一个类似下面的 docker-compose.yml:

networks:
  smstake: 
    ipam:
      config:
        - subnet: 10.0.10.0/24
services:
    app:

        image: smstake:latest
        ports:
          - 8000:80
        networks:
          - smstake

        depends_on:
          - db
        deploy:
          mode: replicated
          replicas: 1
          placement:
            constraints:
              - node.role == manager

I am using it for deploying the service in nodes running in swarm mode.我正在使用它在以 swarm 模式运行的节点中部署服务。

Every time an image is build, the image name may differ based on user passed branch name or tagname which works as tag for the image.每次构建图像​​时,图像名称可能会根据用户传递的分支名称或用作图像标记的标记名而有所不同。 I am running it from the jenkins.我从詹金斯那里运行它。 For eg: smstake:例如: smstake:

How can I dynamically add the image name to the image parameter of the service.如何将图像名称动态添加到服务的图像参数中。 As docker stack does not support build.由于 docker stack 不支持构建。 I cannot even use it.我什至不能使用它。 I am not able to figure out the right way to do it.我无法找出正确的方法来做到这一点。

I am trying to deploy with docker stack deploy -c docker-compose.yml stackname我正在尝试使用docker stack deploy -c docker-compose.yml stackname进行部署

My exact Requirement being:我的确切要求是:

  1. Have a build job in jenkins which builds the image for us.在 jenkins 有一个构建工作,它为我们构建图像。
  2. The image name differs or changes if the tag or branch name changes如果标签或分支名称更改,则图像名称不同或更改
  3. We have a build job to deploy the jobs again with the newly created image.我们有一个构建作业,可以使用新创建的映像再次部署这些作业。

The reason behind creating new image for new TAG is so that I can rollback to previously build image.为新标签创建新映像的原因是我可以回滚到以前构建的映像。

Some edit: Added the image-name to add in configuration.env file which will be passed using echo command in deploy job before deploy command runs.一些编辑:添加图像名称以添加到 configuration.env 文件中,该文件将在部署命令运行之前使用部署作业中的 echo 命令传递。 than the docker-compose will look like following比 docker-compose 看起来像下面

version: '3.4'
networks:
  smstake: 

services:

    db:
        image: mysql:5.7
        networks:
          - smstake
        ports:
          - "3306"
        env_file:
          - configuration.env
        environment:
          MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
          MYSQL_DATABASE: ${DB_NAME}
          MYSQL_USER: ${DB_USER}
          MYSQL_PASSWORD: ${DB_PASSWORD}
        volumes:
          - mysql_data:/var/lib/mysql
        deploy:
          mode: replicated
          replicas: 1

    app:
        env_file:
          - configuration.env
        image: ${SMSTAKE_VERSION}
        ports:
          - 8000:80
        networks:
          - smstake
        depends_on:
          - db
        deploy:
          mode: replicated
          replicas: 1
          placement:
            constraints:
              - node.role == manager
volumes:
    mysql_data:

Why is it not reading from the configuration.env file ,the right value with that key is set there I have confirmed .为什么它没有从 configuration.env 文件中读取,我已经确认在那里设置了该键的正确值。 Error message错误信息

Creating service smstake_app failed to create service smstake_app: Error response from daemon: rpc error: code = InvalidArgument desc = ContainerSpec: image reference must be provided Build step 'Execute shell' marked build as failure Finished: FAILURE创建服务 smstake_app 未能创建服务 smstake_app:来自守护进程的错误响应:rpc 错误:代码 = InvalidArgument desc = ContainerSpec:必须提供图像参考构建步骤“执行外壳”标记构建为失败已完成:失败

In the docker-compose file, you can have variables substitution based on environment variables.在 docker-compose 文件中,您可以根据环境变量进行变量替换。 This is documented under Variable Substitution .这记录在变量替换下。

You can use the following to specify a different version for the image:您可以使用以下内容为图像指定不同的版本:

image: smstake:${SMSTAKE_VERSION}

And inside the jenkins job that deploys, you can just set this environment variable and run the docker stack command:在部署的 jenkins 作业中,您可以设置此环境变量并运行 docker stack 命令:

SMSTAKE_VERSION=v1.2.0 docker stack deploy -c docker-compose.yml stackname

You need to do it in two stages您需要分两个阶段进行

docker-compose config | docker stack deploy -c - stackname

You need to use docker-compose and not the docker compose V2 as docker stack does not support version -less manifests (at least not until the 21.xx version which as of this writing is not out yet)您需要使用docker-compose而不是docker compose V2,因为docker stack不支持无version的清单(至少在撰写本文时尚未发布的 21.xx 版本之前不支持)

You also get the added bonus of using a .env file to read your environments which makes it easy for devs as well.您还可以获得使用.env文件来读取您的环境的额外好处,这也使开发人员也很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM