How to dynamically change the docker-compose image field

Tara Prasad Gurung picture Tara Prasad Gurung · Apr 1, 2018 · Viewed 9.1k times · Source

I have a docker-compose.yml something like bellow:

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.

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:

How can I dynamically add the image name to the image parameter of the service. As docker stack does not support build. 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

My exact Requirement being:

  1. Have a build job in jenkins which builds the image for us.
  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. than the docker-compose will look like following

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 . 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

Answer

yamenk picture yamenk · Apr 1, 2018

In the docker-compose file, you can have variables susbsitution based on environment variables. 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:

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