Docker-compose, conditional statements? (e.g. add volume only if condition)

DockerDocker ComposeConditional StatementsCloudDevops

Docker Problem Overview


I want to add a volume to my service, but only if the final user gave a folder for it. Otherwise, no volume should be mounted, for the already-prepared image has valid data in a default folder.

That is, I want to do something like (pseudocode):

services:

  my_awesome_service:
  
    volumes:
      if ${VARIABLE} => ${VARIABLE}:/app/folder

Are such conditional statements definable in a docker-compose file?

The only way I see to make this possible is to first define a base docker-compose file, which does not have the volume mount, and the call on a second docker-compose file only if the $VARIABLE is defined. This is fine for a single or few conditions, but gets nasty if there are many.

Any solution?

Docker Solutions


Solution 1 - Docker

Poor man's solution:

    volumes:
      ${VARIABLE:-/dev/null}:/app/folder

Or:

    volumes:
      ${VARIABLE:-/dev/null}:${VARIABLE:-/tmp}/app/folder

Solution 2 - Docker

Nothing like this currently exists. Options to implement this that I can come up with include:

  1. Make lots of compose file pieces, and merge together the parts you need to build up the final file.

  2. Dynamically generate your compose file. Something like jsonnet may be a good starting point.

  3. Skip compose, and just dynamically generate your docker run command. This starts to lack portability but some use cases are just easier to script yourself.

  4. Submit a PR to the compose and docker/cli github repos to extend the compose functionality. Doing this with a golang template syntax would make the most sense to me.

Solution 3 - Docker

Yeah, I do not think docker-compose's format supports conditional statements.

However, two solutions could be:

  1. Pass a "complex" (list-like) variable to the docker-compose such as in this example:

docker-compose.yaml:

command: ${COMMAND_PARAMS}

bash:

#!/bin/bash
if test -z $CONDITION; then
  params="-param1 ${MIPARAM1}"
else
  params="-param1 ${MIPARAM1} -param2 ${MIPARAM2}"
fi
COMMAND_PARAMS=$params docker-compose up

(credits goes to original poster on github, @shin-)

  1. Prepare the default folder in the docker image in a folder named something like folder_defaults, then have the volume always defined in docker-compose.yml, but then finally, have an internal script in the docker image that checks whether the volume folder is empty, and if so ln -s to the folder_defaults; otherwise leave it as it is.

Example of the conditional script:

if [ -z "$(ls -A /a/folder)" ]; then
  do something... using /a/folder_defaults
fi

Solution 4 - Docker

If you are using Rancher for orchestration, there are escapes {{...}} available you can use for conditional statements in Rancher's version of docker-compose.

Read more about the integrated GO templating system here.

Solution 5 - Docker

Using conditional stmts in docker-compose is somewhat possible. Checkout variable substitution. The documentation is available for just the simplest of if-else. And since I have not tried with complex expressions involving strings, I cannot be sure. But following are the points you might want to keep in mind when trying out conditional variables:

  • Environment variables in docker-compose file (with only a key) are resolved to their values on the machine Compose is running on. So when using ${CHEESE} in docker-compose, one should have CHEESE="cheddar" set in .env file or exported manually in host machine.
  • Alternatively .env file can be set with env_file option. Variables in this file are exported in the docker container before the variables under environment option. That means variables in environment will override variables in env_file .

Solution 6 - Docker

We can use conditional statement in docker-compose.yml file as below:

#jinja2: lstrip_blocks: True
version: "3.2"
services:
  app-name:
    image: url
    deploy:
      replicas: {{replication-num}}
      resources:
        limits:
          memory: 4G
        reservations:
          memory: 1G
      restart_policy:
        condition: any
        max_attempts: 3
      update_config:
        delay: 60s
        failure_action: rollback
        parallelism: 1
        monitor: 30s
      placement:
        constraints:
          - node.labels.node == worker
    ports:
      - "{{url}}:8000"
    environment:
      ps_environment: "{{env}}"      
    {% if env=='sandbox' %}
    extra_hosts:
      - {{ sandbox_fully }}
    {% endif %}
    secrets:
      - source: pwdFile   
    labels:
      - container_name=app-name
    networks:
      - App_External_Overlay
    volumes:
      - {{ dir }}:{{ dir }}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjuanmirocksView Question on Stackoverflow
Solution 1 - DockergatopeichView Answer on Stackoverflow
Solution 2 - DockerBMitchView Answer on Stackoverflow
Solution 3 - DockerjuanmirocksView Answer on Stackoverflow
Solution 4 - DockerWeSeeView Answer on Stackoverflow
Solution 5 - Dockerkhari-singView Answer on Stackoverflow
Solution 6 - DockerKamta MishraView Answer on Stackoverflow