Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically change the docker-compose image field

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

like image 498
Tara Prasad Gurung Avatar asked Apr 01 '18 10:04

Tara Prasad Gurung


People also ask

Can you use environment variables in docker compose?

In Docker Compose, IT admins can use environment variables to generalize configurations for different situations, deployment environments and security contexts without editing the main project file(s) manually. Variables can either be passed as command-line arguments -- suitable for only a few parameters -- or via a .

How do I set an environment variable in docker?

Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.

What is Depends_on in docker compose?

depends_on is a Docker Compose keyword to set the order in which services must start and stop. For example, suppose we want our web application, which we'll build as a web-app image, to start after our Postgres container.


1 Answers

In the docker-compose file, you can have variables substitution 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
like image 146
yamenk Avatar answered Sep 30 '22 10:09

yamenk