Skip to content

What is the easiest way to globally enable optional .env.local in Docker Compose? #12585

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

Open
0x131315 opened this issue Feb 25, 2025 · 2 comments

Comments

@0x131315
Copy link

In Makefile i use this way:

include .env
-include .env.local
-include local.mk
export

//other code

What is the easiest way to GLOBALLY enable OPTIONAL .env.local in Docker Compose?

In issues and official docs i have found the following options, but they are not simple:

  • env_file - support optional files, but not globally, requires too many edits when there are a lot of services
  • COMPOSE_ENV_FILES - globally, but does not support optional files

I use Docker Compose version 2.32.4

@ndeloof
Copy link
Contributor

ndeloof commented Feb 26, 2025

There's no such mechanism offered by Docker Compose.
.env is the sole supported source to define variables to be applied for interpolation.

@0xShubhamSolanki
Copy link

0xShubhamSolanki commented Apr 10, 2025

As of Docker Compose version 2.32.4 (released after February 2025, aligning with the current date of April 10, 2025), there isn’t a built-in, straightforward way to globally enable an optional .env.local file across all services without some customization. However, based on your requirement and the constraints you’ve outlined (avoiding per-service env_file edits and the limitations of COMPOSE_ENV_FILES), here’s the easiest practical approach:

Easiest Solution: Use a Shell Wrapper with COMPOSE_ENV_FILES
You can leverage the COMPOSE_ENV_FILES environment variable in combination with a simple shell script or Makefile to make .env.local optional and globally applied. This avoids modifying the docker-compose.yml file for each service and works with your existing setup.

Steps:
Modify Your Makefile (or Create a Shell Script):
Update your existing Makefile to dynamically include .env.local if it exists, and set it in COMPOSE_ENV_FILES. Here’s how you can adapt it:

makefile

include .env -include .env.local export ifneq ($(wildcard .env.local),) export COMPOSE_ENV_FILES=.env,.env.local else export COMPOSE_ENV_FILES=.env endif up: docker compose up

Run Your Command:
Use the Makefile to start Docker Compose:

make up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants