Sub topic 7
Configuring services with environment variables and .env files
This chapter assumes you can read a compose.yaml, know what a service is, and have run docker compose up. It covers how values reach a Compose file and how they reach a container. These are two separate steps, and mixing them up causes most of the common errors.
Two kinds of variables
Compose handles variables in two places:
- Interpolation happens when Compose reads the file. Text such as
${DB_PASSWORD}is replaced before any container exists. - Container environment is the set of variables the process inside the container sees. You set it with
environment:orenv_file:.
A value in your shell or .env file does not enter a container unless the Compose file passes it in.
The .env file
Compose reads a file named .env in the project directory and uses it to fill in ${...} references.
# .env (read automatically from the project directory)
DB_PASSWORD=change-meHere is a service that uses it:
name: shop
services:
db:
image: postgres:16
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: appdocker compose config prints the file with every value filled in. Run it to check what Compose resolved.
Interpolation syntax
| Syntax | Meaning |
|---|---|
| `${VAR}` | The value of `VAR`, or an empty string if unset (Compose prints a warning) |
| `${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 `$` |
Prefer ${DB_PASSWORD:?set DB_PASSWORD in .env} for required values. The error appears at startup instead of as a confusing database failure.
Precedence
When the same name is set in several places, interpolation uses this order, highest first:
- Variables in your shell environment
- Values from
--env-fileon the command line - The
.envfile
So DB_PASSWORD=other docker compose up overrides .env.
Inside the container, the order differs. A value in environment: beats one from env_file:. Both beat a value baked into the image with ENV.
Passing values into a container
There are three ways to do it:
services:
web:
build: .
env_file:
- ./web.env
environment:
LOG_LEVEL: info
DATABASE_URL: postgres://app:${DB_PASSWORD}@db:5432/app
API_KEY:environment:sets named values. A key with no value, likeAPI_KEY, copies the value from your shell.env_file:loads every line of a file into the container. These values are not used for interpolation.${...}fills in values inside either one.
Common errors
- A warning that a variable is not set. The
.envfile is in the wrong directory, or the name is misspelled. Compose looks in the project directory, not where you ran the command from if you used-for--project-directory. - A value you expect from
.envis missing in the container. You never listed it underenvironment:. Interpolation alone does not pass it. - A stale value. A variable exported in your shell overrides
.env. Runenv | grep DB_to look for it. - Quotes in
.env. Compose strips surrounding quotes, but spaces around=can cause surprises. WriteKEY=value. - Secrets committed to git. Add
.envto.gitignoreand commit a.env.exampleinstead.
Exercise
Your compose.yaml uses ${LOG_LEVEL:-warn} and .env contains LOG_LEVEL=debug. You run LOG_LEVEL=error docker compose up. Which value is used, and what does the web container see if the service has no environment: entry for it?
Show answer
Interpolation uses error, because the shell beats .env, and .env beats the default warn. If no environment: or env_file: entry passes the value, the container does not see LOG_LEVEL at all. Interpolation only fills in the Compose file.