Skip to content

Instantly share code, notes, and snippets.

@x-yuri
Last active October 26, 2019 07:00
Show Gist options
  • Select an option

  • Save x-yuri/9a8092c04df073afc203dc8f8401d9ef to your computer and use it in GitHub Desktop.

Select an option

Save x-yuri/9a8092c04df073afc203dc8f8401d9ef to your computer and use it in GitHub Desktop.
docker-compose.yml: variables

In docker-compose.yml one can use variables either from environment, or from .env file. They don't automatically propagate into the containers. Variables added with env_file option are available only in the containers. environment option overrides values from env_file.

docker-compose.yml:

version: '3'

services:
  bash:
    image: bash
    entrypoint: sleep 100000000
    env_file:
      - .env.custom
    environment:
      A: $A  # from .env
      B: 7
      C: $C  # from environment
      E: 8   # override .env.custom
      F: $F  # unset, override .env.custom

.env:

A=1
B=2

.env.custom:

D=4
E=5
F=6
$ C=3 docker-compose up
WARNING: The F variable is not set. Defaulting to a blank string.
Recreating 5_bash_1 ... done
Attaching to 5_bash_1

$ C=3 docker-compose exec bash bash -c set | egrep '^(A|B|C|D|E|F)='
The F variable is not set. Defaulting to a blank string.
A=1  # from .env
B=7  # from docker-compose.yml
C=3  # from environment
D=4  # from .env.custom
E=8  # from docker-compose.yml
F=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment