Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dimadeush opened this issue Oct 19, 2022 · 3 comments
Labels
area/env .env and env_file kind/feature

Comments

@dimadeush
Copy link

dimadeush commented Oct 19, 2022

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

@ndeloof
Copy link
Contributor

ndeloof commented May 15, 2023

this is slightly different but close to #9181

@jhrotko jhrotko added the area/env .env and env_file label Oct 14, 2024
@ndeloof
Copy link
Contributor

ndeloof commented Nov 8, 2024

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>

@dimadeush
Copy link
Author

dimadeush commented Nov 9, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/env .env and env_file kind/feature
Projects
None yet
Development

No branches or pull requests

3 participants