Sub topic 9
Parameterizing with variables and env files
This chapter assumes you can read Compose YAML, run docker compose up, and set shell environment variables. It covers how Compose substitutes values into your files, so the same files can serve several environments.
Interpolation in Compose files
Interpolation means Compose replaces ${NAME} in a file with a value before it builds the configuration. Compose reads these values from the shell environment and from env files.
You have already seen it in the base file: POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD}. The syntax has a few forms:
| Syntax | Meaning |
|---|---|
| `${VAR}` | Value of `VAR`. Empty string, with a warning, if unset. |
| `${VAR:-default}` | `default` if `VAR` is unset or empty. |
| `${VAR-default}` | `default` only if `VAR` is unset. |
| `${VAR:?message}` | Stop with `message` if `VAR` is unset or empty. |
| `$$` | A literal `$`. |
Use :- for harmless settings such as image tags. Use :? for values that must never be guessed, such as passwords.
# compose.yaml (excerpt)
services:
web:
image: example/web:${WEB_TAG:-1.0}
ports:
- "${WEB_PORT:-8000}:8000"
environment:
LOG_LEVEL: ${LOG_LEVEL:-info}
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD}Env files
An env file is a plain text file of KEY=value lines. By default Compose reads a file named .env in the project directory and uses it for interpolation.
# .env (not committed if it holds secrets)
WEB_PORT=8000
LOG_LEVEL=debug
DB_PASSWORD=dev-only-passwordTo use a different file, pass --env-file. You can repeat it, and later files win.
docker compose --env-file .env --env-file .env.prod configThis gives you one env file per environment, with the same YAML underneath.
Which value wins
When the same variable is defined in several places, Compose picks one in this order, highest priority first:
- A variable set in your shell.
- A value from a file passed with
--env-file. - A value from the default
.envfile. - The default inside
${VAR:-default}.
# The shell value beats .env, so this runs on port 9000
WEB_PORT=9000 docker compose config`.env` versus `env_file`
These two look alike but do different jobs.
- The project
.envfile and--env-filefeed interpolation, which fills${...}in the Compose file itself. - The
env_file:attribute on a service passes variables into the container at runtime.
services:
web:
image: example/web:1.0
env_file:
- ./web.envA variable in web.env is visible inside the container. It is not available for ${...} substitution in the YAML. Mixing these up is a common source of "variable is not set" warnings.
Using variables with overrides and profiles
Variables pair well with the layering from earlier chapters. A shared file holds ${LOG_LEVEL:-info}, and each environment's env file changes only the values.
docker compose --env-file .env.prod -f compose.yaml -f compose.prod.yaml configRun config first. It prints the merged file with every variable already substituted, so you can confirm the final values before starting anything.
Exercise
Exercise
Your compose.yaml contains image: example/web:${WEB_TAG:-1.0}. .env sets WEB_TAG=1.2, and you run WEB_TAG=1.3 docker compose --env-file .env.prod config, where .env.prod sets WEB_TAG=2.0. Which tag appears in the output, and why?
Show answer
1.3. A variable set in the shell has the highest priority, above --env-file values, the default .env file, and the inline default.