I was playing around with args and env_file and realised the env_file does not work in args as it is stated in the docs: https://docs.docker.com/compose/compose-file/#envfile
Interestingly it DOES WORK if the env_file is called .env which is the default name. It seems this file is loaded earlier.
My docker-compose file looks as following:
# This should test the environment with the latest snapshots
# This is based on base.yml
version: '2'
services:
elasticsearch:
env_file:
- snapshot.env
build:
context: Dockerfile
args:
TEST: ${TEST}
If I run docker-compose config I get the following output:
WARNING: The TEST variable is not set. Defaulting to a blank string.
networks: {}
services:
elasticsearch:
build:
args:
TEST: ''
context: /Users/ruflin/Dev/tmp/compose-testing/Dockerfile
environment:
TEST: abcd
version: '2.0'
volumes: {}
If I now change the line - snapshot.env to -.env, rename also my env file and run it again, I get:
networks: {}
services:
elasticsearch:
build:
args:
TEST: abcd
context: /Users/ruflin/Dev/tmp/compose-testing/Dockerfile
environment:
TEST: abcd
version: '2.0'
volumes: {}
This brings up two questions from my side:
- Why does
.envwork andsnapshot.envdoesn’t? - Is there an option to specific env variables for args in a separate file?
The problem with .env auto discovery is that in case I have a second compose file which extends this one in a different directory, it will not interpret the .env file for the first composer file.