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=