Docker Compose env_file, environment and .env: Which Does What
In Docker Compose, env_file loads a file of KEY=value lines into one service's container. The environment attribute sets container variables inline and beats env_file when both name the same key. The .env file beside compose.yaml is different: Compose reads it to fill ${VAR} placeholders in the YAML itself.
| Mechanism | Scope | Reaches the container? |
|---|---|---|
| .env next to compose.yaml | Interpolation of ${VAR} in the Compose file | Only if a service references it |
| env_file: on a service | That service's container | Yes, every line |
| environment: on a service | That service's container | Yes, and it beats env_file |
| --env-file on the CLI | Replaces the default .env for interpolation | Only if referenced |
Updated
The confusion comes from the name: a file called .env and an attribute called env_file sound like the same thing, but one configures the Compose file and the other configures a container. Use the resolver to see which source wins for a variable, then try the interpolation forms against a set, empty or unset variable.
Which value wins?
Variable APP_MODE is set in the places ticked below.
- docker compose run -e
- environment or env_file, value interpolated from the shell or .env
- environment attribute (literal value): Value production
- env_file attribute: Value development
- ENV in the image: Value default
The container gets APP_MODE=production from: environment attribute (literal value)
Interpolation tester
Expression: ${VAR:-default}. VAR is set but empty.
Result: default
Every interpolation form
| Form | VAR=abc | VAR empty | VAR unset |
|---|---|---|---|
| ${VAR} or $VAR | abc | empty | empty, with a warning |
| ${VAR:-default} | abc | default | default |
| ${VAR-default} | abc | empty | default |
| ${VAR:?msg} | abc | error msg | error msg |
| ${VAR?msg} | abc | empty | error msg |
| ${VAR:+other} | other | empty | empty |
| ${VAR+other} | other | other | empty |
| $$ | a literal $ | a literal $ | a literal $ |
Compose files you can copy
env_file plus environment overrides
Shared settings come from files; the environment entries win for the keys they name. The local file is optional.
services:
api:
image: node:20
env_file:
- path: ./api.env
- path: ./api.local.env
required: false
environment:
NODE_ENV: production
LOG_LEVEL: ${LOG_LEVEL:-info}
SHOW_DOCS: "false"Project .env used only for interpolation
These two values fill placeholders in compose.yaml. Neither reaches a container unless a service mentions it.
# .env (next to compose.yaml)
POSTGRES_TAG=16
WEB_PORT=8080Using those values in compose.yaml
The image tag and the published port come from .env; the password must be set or Compose stops with a clear message.
services:
db:
image: postgres:${POSTGRES_TAG:-16}
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD in .env}
web:
image: nginx
ports:
- "${WEB_PORT:-80}:80"Check what Compose resolved
config prints the final model with every variable filled in; --environment lists the values used for substitution.
docker compose config
docker compose config --environment
docker compose --env-file ./staging.env config
The precedence list, top to bottom
Docker documents five levels. A value passed with docker compose run -e is strongest. Next comes environment or env_file where the value is interpolated from your shell or an environment file, then a literal value under environment, then a value from env_file, and last an ENV line baked into the image.
Notice that only one of these is the .env file, and only indirectly: it feeds level two through interpolation. A value sitting in .env that no service mentions never appears inside a container.
Keys without values
A bare name under environment, such as - API_TOKEN with no equals sign, copies the value from the shell running Compose. If the shell does not have it, the variable is left unset.
In an env_file, a line holding only a key unsets that variable and removes it from the container environment, which is handy for deliberately clearing something the image sets.
How it works
- Tick each place where your variable is set and type the value it has there.
- The resolver walks Docker's documented precedence list from the top and shows which value the container ends up with.
- In the interpolation box, pick a form such as ${VAR:-default}, choose whether VAR is set, empty or unset, and read the result.
- Run docker compose config in your project to confirm what Compose actually resolved.
Tips
- Run docker compose config --environment to print the variables Compose used for interpolation. It settles most arguments in seconds.
- Mark optional env files with required: false (Compose 2.24.0 and later) so a missing local override does not stop the stack.
- Write $$ when you need a literal dollar sign in compose.yaml, for example in a password or a shell command.
- Quote booleans in the environment map, like SHOW: "true", so YAML does not turn them into true and false values.
Frequently asked questions
What is the difference between env_file and environment?
Both put variables into a service's container. env_file reads them from one or more files, environment lists them in compose.yaml. When the same key appears in both, the environment value wins, even if it is empty.
Does Compose pass my .env file into containers automatically?
No. The project .env file is only used to substitute ${VAR} in the Compose file. A variable reaches a container only when a service names it under environment, or loads the file with env_file.
Where does Compose look for the .env file?
In the project directory, which is the folder given with --project-directory, else the folder of the first -f file, else your current folder. Use --env-file to point at another file; with several, later files override earlier ones.
Are env_file paths relative to where I run the command?
No. Relative paths in env_file are resolved from the folder that holds the Compose file, so the stack works no matter where you start it.
What happens to ${VAR} when VAR is not set?
Compose prints a warning and substitutes an empty string. Use ${VAR:-fallback} to supply a default, or ${VAR:?message} to stop with an error instead.
Related: .env file format, Next.js environment variables, Deploy a React app, Git cheat sheet.
Build your site free or see all free tools. On We.Inc you can build your site free, with no card and no trial needed to start. AI building is on paid plans from $19 a month.
More in Guides
- Domain Forwarding: Send One Domain to Another the Right Way
- Domain Management for Agencies: Ownership, Access and Renewals Without the 2 a.m. Outage | How-to Guide
- Domain vs Hosting: What Is a Domain Name and How Is It Different? | How-to Guide
- Double Opt-In: The Confirm-Your-Email Step
- Duplicate Without User-Selected Canonical (and Alternate Page With Proper Canonical Tag)
- Embed Google Forms, Calendly and Typeform on Your Website
- How to Embed a Google Map on Your Website
- Emergency Alerts on Government Websites: Banners, Pages and a Playbook for the Worst Day | How-to Guide
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App