Hi,
I am trying to optimise my setup for different environments using multiple docker-compose..yml files and also multiple ..env files. But I am not successful.
My docker-compose.yml is my base compose file that is used on every environment. For each environment I have defined an override file such as docker-compose.linode.yml. I will just list ports for demonstration purposes and the parts regarding env variables as that is the area where I can’t get it to work.
## docker-compose.yml
services:
myapp:
ports:
- 8090:8090
environment:
FA_SERVER: ${FA_SERVER}
## docker-compose.linode.yml
## override ports & .env files
services:
myapp:
ports:
- 80:8090
env_file:
- .env
- .linode.env
environment:
FA_SERVER: ${FA_SERVER}
## base .env
FA_SERVER=LOCAL
...
## .linode.env (override base .env)
FA_SERVER=LINODE
...
Locally I will just spin it up with (no problems here)
docker-compose up -d
FA_SERVER will evaluate to LOCAL.
Then on my Linode VPS I would do:
docker-compose -f docker-compose.yml -f docker-compose.linode.yml up -d
FA_SERVER still evaluates to LOCAL isntead of LINODE.
So my expectation is, that through the docker-compose.linode.yml the two .env files are evaluated with the one listed below having priority (i.e. .linode.env) as per the documentation:
https://docs.docker.com/compose/compose-file/compose-file-v3/#env_file
Keep in mind that the order of files in the list is significant in determining the value assigned to a variable that shows up more than once . The files in the list are processed from the top down. For the same variable specified in file
a.env
and assigned a different value in fileb.env
, ifb.env
is listed below (after), then the value fromb.env
stands. For example, given the following declaration indocker-compose.yml
and https://docs.docker.com/compose/environment-variables/#the-env_file-configuration-option
When you set the same environment variable in multiple files, here’s the priority used by Compose to choose which value to use:
- Compose file
- Shell environment variables
- Environment file
- Dockerfile
- Variable is not defined
The idea is to simplify the commands to spin up the containers with compose and not having to use also arguments for the env files. I also believe docker-compose up
does not support cli arguments for env files, while docker-compose run
does not support cli arguments to specify multiple compose files.
Thanks for help in advance!