compose: [BUG] environment variable not correctly overload when using multiple .env files
Description
The documentation example (https://docs.docker.com/compose/environment-variables/set-environment-variables/#additional-information) for overloading environment variables using multiple .env files doesn’t seem to work as expected with compose version 2.27.0.
Steps To Reproduce
Use these files
.env
MYVAR=Default
.env.override
MYVAR=Override
docker-compose.yml
services:
shell:
image: ubuntu:22.04
env_file:
- path: .env
required: true
- path: .env.override
required: true
environment:
- DOCKER_EXPORTED_VAR=${MYVAR}
entrypoint: /bin/bash
Run compose config command
$ docker compose config
name: testdocker
services:
shell:
entrypoint:
- /bin/bash
environment:
DOCKER_EXPORTED_VAR: Default
MYVAR: Override
image: ubuntu:22.04
networks:
default: null
networks:
default:
name: testdocker_default
See how MYVAR is correctly overridden by the .env.override file as it is exported in the report log. But the DOCKER_EXPORTED_VAR is not using the overload.
Run another compose config command but forcing env files from command line
$ docker compose --env-file .env --env-file .env.override config
name: testdocker
services:
shell:
entrypoint:
- /bin/bash
environment:
DOCKER_EXPORTED_VAR: Override
MYVAR: Override
image: ubuntu:22.04
networks:
default: null
networks:
default:
name: testdocker_default
This time, both variables are correctly exported.
Not what the documentation says:
This section of the documentation clearly says that when using multiple files, they are loaded in sequence and that when the same variable is defined multiple times, the last definition wins:
Compose Version
$ docker compose version
Docker Compose version v2.27.0-desktop.2
$ docker-compose version
Docker Compose version v2.27.0-desktop.2
Docker Environment
No response
Anything else?
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 20 (9 by maintainers)
Ahhh I see what you’re saying… Dang it.
I still think env_files should be used for interpolation, but this seems like a fight I don’t think I’d win.
Again,
env_fileattribute in your compose file is NOT used to interpolate variables asFOO=\${FOO}. To get interpolation usedocker compose --env-file ../env-global --env-file env-local xxxThen,
environmenthas precedence overenv_filesoFOOandBARwill be set according to value computed by interpolation, as as logged in warning “_The “FOO” variable is not set. Defaulting to a blank string. _” so you eventually get FOO=“” as demonstrated by your command.