compose icon indicating copy to clipboard operation
compose copied to clipboard

Proposal to change logic for loading env file for the docker-compose (--env-file)

Open dimadeush opened this issue 3 years ago • 4 comments

Hello Docker Team

Thank's for the great products Docker and Docker compose.

Currently PHP Symfony framework has next logic for loading .env files:


# In all environments, the following files are loaded if they exist,
# the latter taking precedence over the former:
#
#  * .env                contains default values for the environment variables needed by the app
#  * .env.local          uncommitted file with local overrides
#  * .env.$APP_ENV       committed environment-specific defaults
#  * .env.$APP_ENV.local uncommitted environment-specific overrides

# Real environment variables win over .env files.

When using docker-compose, it loads .env file by the default, but we are able to use --env-file option in order to specify path for docker envs variables. If file missing then error provided and we even can't build containers. Such logic leads to the situation, when we want to override some variables, we need to have a separate .env file for the docker(f.e. docker/.env) and this file should be "uncommitted file". It means, that at the same time, we need to have some .env.example file(docker/.env.example) for the docker that committed. So, after such actions, every time, after pulling new changes from the git, every developer need to check if we have some new envs inside docker/.env.example and copy new values into uncommitted file docker/.env (f.e. some of the team-member has MacOS and he need to change some env variable without committing it).

We have proposal do not have different .env files for the Symfony and docker-compose(.env + docker/.env) if we already have one in the parent folder of the project. We have proposal to ask you to add additional option for the docker-compose, like --env-file-if-exists=... and in such case we can have the same behavior for the symfony framework and docker-compose. So final docker-compose instruction inside Makefile can be like next:

docker-compose --env-file .env --env-file-if-exists .env.local --env-file-if-exists .env.dev --env-file-if-exists .env.dev.local -f docker-compose.yml build

With hope Thanks in Advance

dimadeush avatar Oct 19 '22 10:10 dimadeush

this is slightly different but close to https://github.com/docker/compose/issues/9181

ndeloof avatar May 15 '23 13:05 ndeloof

every developer need to check if we have some new envs

You should declare variables in your compose.yaml with either a default value or error:

  value: ${VAR:?must declare a value in .env file}
  another: ${VAR:-some reasonable default value}

Doing so users will be warned if they need some local settings to be declared in env file

docker-compose instruction inside Makefile can be like next:

As you use a Makefile, you can include some conditional code to detect the adequate env file flags to be used:

ifneq ("$(wildcard $(.env.local))","")
    ENVFILE = "--env-file .env --env-file .env.local "
else
    ENVFILE = "--env-file .env"
endif

command: 
    docker compose $ENVFILE <command>

ndeloof avatar Nov 08 '24 12:11 ndeloof

As you use a Makefile, you can include some conditional code to detect the adequate env file flags to be used:

Hello @ndeloof

Thanks for response and for the example.

Yes, it's like workaround. But we can have more difficult case, f.e. when .env.local is not exists but exists .env.dev.local or vice-versa or both files exists or exists .env -> .env.local -> .env.dev -> .env.dev.local. In additional such workaround has dependency on Make tool (sudo apt install make) and OS type.

Maybe it will be great to have native support by the docker compose utility and option like --env-file-if-exists. P.S. WDYT?

With hope Thanks in Advance

dimadeush avatar Nov 09 '24 14:11 dimadeush

Hello

Hope that someday it will be implemented, thanks in advance.

dimadeush avatar Apr 17 '25 11:04 dimadeush

env file management is a risky area, we got a bunch of regression any time we introduced changes there, even those considered reasonable. For this reason, I'm closing this as "not planned" - it you want to have a more flexible env variable management, I suggest to use a Makefile or script to setup env before you run compose commands

ndeloof avatar Dec 16 '25 09:12 ndeloof