Sub topic 3
Loading container variables from files with env_file
This chapter assumes you can write a basic compose.yaml, run docker compose up, and set shell variables. It also assumes you know the difference between values Compose substitutes into the file (interpolation) and values passed into a container.
Why use env_file
The environment attribute works for a handful of variables. A service with 20 settings makes the Compose file hard to read. env_file moves those settings into separate files. Compose reads each file and sets the variables in the container's environment.
env_file only configures the container. It does not supply values for ${VAR} expressions in the Compose file. That job belongs to the .env file and the shell, covered in other chapters.
Short and long syntax
Use the short form for one file, or a list for several:
services:
app:
image: my-app:latest
env_file: common.envUse the long form when you need more control:
services:
app:
image: my-app:latest
env_file:
- common.env
- path: local.env
required: falseWith required: false, Compose skips the file if it does not exist. Without it, a missing file stops docker compose up with an error. This suits developer-specific overrides that are not committed to Git.
Relative paths are resolved from the directory containing the Compose file.
A worked example
Create two files next to compose.yaml.
# common.env
APP_MODE=production
LOG_LEVEL=info# local.env
APP_MODE=developmentReference both files, and set one variable again in environment:
services:
app:
image: my-app:latest
env_file:
- common.env
- path: local.env
required: false
environment:
LOG_LEVEL: debugStart the service, then list the variables inside the container:
docker compose up -d
docker compose exec app envThe output includes APP_MODE=development and LOG_LEVEL=debug. Two rules explain this:
- Files are read in the order listed, and a later file overrides an earlier one.
local.envwins overcommon.env. - A value in
environmentoverrides the same name from anyenv_file.
Variables with no value
In environment, a name with no value, such as API_TOKEN:, takes its value from the shell where Compose runs. This avoids writing the secret in a file. If the variable is not set in the shell, it is left unset in the container.
File contents
Each line in an env file is NAME=value. Lines starting with # are comments, and blank lines are ignored. Quoting a value with double quotes keeps surrounding spaces. Single quotes keep the value literal. Compose also expands ${VAR} references in unquoted and double-quoted values, so a $ in a password may be consumed unexpectedly. Single-quote such values.
Common mistakes
| Symptom | Likely cause |
|---|---|
| `${VAR}` in `compose.yaml` is blank, though `VAR` is in `common.env` | `env_file` does not feed interpolation. Put the value in `.env` or the shell. |
| A value differs from the file | The same name is set in `environment`, or in a later file. |
| `env file ... not found` error | The path is wrong, or the file is optional but lacks `required: false`. |
Exercise
A service lists env_file: [a.env, b.env]. Both files define PORT, with PORT=1 in a.env and PORT=2 in b.env. The service's environment section also sets PORT: 3. What value does the container see, and what would it see if you removed the environment entry?
Show answer
The container sees PORT=3, because environment overrides every env file. Without that entry it sees PORT=2, because the later file, b.env, overrides the earlier one.