compose: [BUG] Overriding environment variables with `-e` doesn't work with docker compose up
Description
The documentation at https://docs.docker.com/compose/environment-variables/set-environment-variables/#substitute-with---env-file states:
You can override specific environment variables from the command line when starting containers.
docker compose --env-file .env.dev up -e DATABASE_URL=mysql://new_user:new_password@new_db:3306/new_database
However, attempting to run docker compose up -e FOO=test results in the error: unknown shorthand flag: 'e' in -e
Steps To Reproduce
- Have a working docker compose set up
- Attempt to provide an environment variable to up with the
-eshorthand syntax:docker compose up -e FOO=test - Witness error
Compose Version
v2.23.3
Docker Environment
Client:
Version: 25.0.3
Server:
Server Version: 25.0.3
Anything else?
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 6
- Comments: 16 (7 by maintainers)
@ndeloof I don’t see how one can argue that
-eshould not apply toupand simultaneously support that an environment file should.An .env file that contains
FOO=testis exactly the same as-e FOO=test. An env file is used with multiple containers;-eshould apply the same way (and override values in the env-file). Do you know of an argument that justifies keeping env-file working forupbut not the-eflag?Perhaps the --env/-e could be added to the main set of compose options. It would allow to easily overwrite a variable, without relying on the shell (and I think Windows’s shell/cmd might be the culprit).
It would complement
docker compose --env-file <...>and allow customizing individual variables.That’s a documentation issue,
updoes not allow setting additional environment variables this way, this option is forruncompose subcommand. The reason is thatuptarget multiple containers, so there’s no simple way to express one want to pass an environment variable to a specific service. Can you please give me more details on your original issue and the need for this ?OK. I understand the implications for
run. But it still seems like-ecould apply toupin the same way as shell variables and environment files are used for interpolation.If I have an .env that has
API_KEYin it and my compose yaml uses it for a couple of containers, it seems reasonable that runningup -e API_KEY="..."could (should) be treated the same way as changing the value in the .env file.Seems straight forward to go with the last provided value.
I don’t see the distinction. One could (& from the genesis of this issue, some do) apply the same “it’s for interpolation” argument to the
-eflag. If a container in a compose file includes an environment variable and an environment file can be used to interpolate those values, the same applies to the-eflag…just do:
shell environment have higher precedence setting the project reference variables