-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Allow env_files in docker-compose files to be optional #9181
Comments
Seems to me there's a confusion here between .env file used to pass variables to compose, and The recommended way to support "optionally set or override environment variables" is to declare those as service service:
web:
- SOME_VAR if |
Assuming we want to add support for "optional env_file" as you describe, this should be debated under Compose specification, and probably will better be addressed with a custom syntax to mark file as optional, maybe using elvis operator ?: service:
web:
env_file:
- .env
- ?:.env.local # optional file |
I'm interessed by this feature for same motivations (working IllustrationLet's look at this code for example (inspired from this one). # docker-compose.yml
services:
php:
environment:
DATABASE_URL: postgresql://${POSTGRES_USER:-api-platform}:${POSTGRES_PASSWORD:-!ChangeMe!}@database:5432/${POSTGRES_DB:-api}?serverVersion=${POSTGRES_VERSION:-13}
database:
image: postgres:${POSTGRES_VERSION:-13}-alpine
environment:
- POSTGRES_DB=${POSTGRES_DB:-api}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-!ChangeMe!}
- POSTGRES_USER=${POSTGRES_USER:-api-platform} If someone modify Since there is no ability to define global constants in compose files and yaml anchors variables are not expanded, I tried using # docker-compose.yml
services:
php:
environment:
DATABASE_URL: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@database:5432/${POSTGRES_DB}?serverVersion=${POSTGRES_VERSION}
database:
image: postgres:${POSTGRES_VERSION}-alpine
environment:
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_USER=${POSTGRES_USER} # .env
POSTGRES_DB=api-platform
POSTGRES_PASSWORD=!ChangeMe!
POSTGRES_VERSION=13
POSTGRES_USER=api-platform This solution works great ! But I came out to the same issue when I tried to commit this |
Thanks @ndeloof , this is helpful. The above example really summarizes this issue with:
Also:
So it seems like this is somewhat doable as long as defaults aren't provided. That seems to break the 80% case of working "out of the clone" without additional configuration. I think your proposal with the |
What about allowing glob patterns such as service:
web:
env_file:
- '.env(.local|)' |
Please, do not try to include some pseudo-programing langage / expression in compose file format :P |
Hi I really want to have this option. env_file:
- path: ''./web/.env.development.local'
required: false
- path: ''./web/.env.local'
required: true How about this format. The advantage of this is capable of many env_files while some is not required and others are required. It happens many times when you have Unit Test CI while you also have E2E Test CI. If maintainer agrees with it I could create PR. |
This should first be debated on compose-specification |
yes, i saw you said that in previous comment as well, but no issue is submitted in compose-specification yet. so i wondered if there is still a problem. you said 'this should be debated in compose-specification' but does it mean 'please post a issue in compose-specification' or 'please wait' ? i'm just confused. |
@ulwlu compose spec is public, anyone can open a proposal. If you feel this is un important topic, you're welcome to do |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
What about allowing glob patterns such as
this would match both files |
This would allow for multiple env files sure, but it doesn't help with the order of precedence so the only way to override the "global" .env is to make sure the local env file is alphabetically sorted first. |
yes please! |
please ! |
please! |
This issue causes problem using vscode devcontainers and codespaces. Without the .env file the container can't be built. When I add an .env to git there's (afaik) no way to tell git to ignore future changes to that file, so the developer has to manually make sure not to accidentally commit secrets and the codespace will be listed as having uncommitted changes. |
The Compose Spec has accepted this change in compose-spec/compose-spec#240 I suggest refrain from discussing changes to the format/syntax or the reasons we need this (we all have them!) as it is now a matter of the spec implementation in this repo. |
Is it working? Mine doesn't work: compose.yml:
Output error:
Docker version:
Docker compose version:
|
This is available in Docker Compose v2.24 and later |
* Allow docker env override via .env.local * Make .env.local optional * Fix env var expansion ignoring .env.local * Rename .env.development to .env.docker * Use YAML anchors * Revert rename of .env.development
Description
As a development leader, I would like teams to be able to spin up local
docker-compose
based environments without steps on the host beyondgit clone...
. As well, developers need to be able to optionally set or override environment variables such as connection strings or secrets. Since overriding variables is the 20% case, most developers should be able to run:One of the most common patterns is to have a checked-in
.env
file of defaults, with.env.local
or similar overriding the first file. Ideally, this could be achieved with:However, docker-compose requires
.env.local
to exist, and errors out. This is especially problematic with VS Code's "clone in named volume" functionality (key for working around Docker performance issues on macOS and Windows) in the remote containers extension, as there's no opportunity to run a script to create even a stub file before containers are started.Additional information you deem important (e.g. issue happens only occasionally):
There was a previous declined PR at #3955 from 2017, along with a related issue at #3560. However, the PR has many comments since closing detailing common use cases and hacks around this missing functionality, as well as comparison to existing tools that do treat
.env
files as optional. I'm opening this issue for current feedback from current maintainers, as it seems that the original closer no longer works at Docker. I've likely missed suggestions and edge cases around this feature expressed in the PR comments, so please don't take the above as a complete description of community needs.Open questions
services
optional that were eventually solved by profiles. I know that wasn't obvious to me until the profiles feature actually landed, so I have some hope there's something in compose v2 to address this.The text was updated successfully, but these errors were encountered: