Sub topic 1
Two jobs for variables: filling in the Compose file and configuring containers
Prerequisites
This chapter assumes you can:
- Explain what images, containers, and published ports are.
- Read and write a simple
compose.yamlwith services. - Set a shell environment variable, for example
export DB_PORT=5433. - Run
docker compose upanddocker compose down.
Two separate jobs
Docker Compose uses the word "variable" for two different things. Mixing them up causes most of the confusing behavior you will see later.
- Interpolation happens in the Compose file, before anything starts. Compose replaces expressions such as
${DB_PORT}with values. The result is a finished configuration. - Container environment happens at runtime. Compose sets environment variables inside the container, and the process in the container reads them.
Interpolation is text substitution in compose.yaml. Container environment is data given to a running process. A value can serve one job, the other, or both, depending on where you put it.
Where each source fits
| Source | Job it does by default |
|---|---|
| `.env` file in the project directory | Supplies values for interpolation |
| Variables in the shell running Compose | Supplies values for interpolation |
| `environment` attribute | Sets variables inside the container |
| `env_file` attribute | Sets variables inside the container |
The .env file does not put its variables into the container. A line like DB_PORT=5432 in .env is only available to ${DB_PORT} expressions in the Compose file. The container sees it only if you pass it on.
A worked example
Suppose .env sits next to compose.yaml:
# .env (in the project directory, next to compose.yaml)
POSTGRES_USER=app
POSTGRES_PASSWORD=change-me
DB_PORT=5432The Compose file uses those values both ways:
services:
db:
image: postgres:${POSTGRES_TAG:-16}
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD must be set}
ports:
- "${DB_PORT:-5432}:5432"Both jobs appear here:
imageandportsare interpolated. The container never seesPOSTGRES_TAGorDB_PORT. They only decide which image to pull and which host port to publish.environmentis interpolated first, and the results are then set in the container. Inside the container,POSTGRES_USERisappandPOSTGRES_PASSWORDischange-me. The Postgres image reads these when it starts.
The :-16 supplies a default and :? stops with an error when a value is missing. Later chapters cover that syntax in detail.
Loading container variables from files
When a service needs many variables, list them in a file with env_file instead of writing each one under environment:
services:
app:
image: my-app:latest
env_file:
- common.env
- path: local.env
required: false
environment:
LOG_LEVEL: debug
API_TOKEN:Here env_file loads container variables from common.env. The local.env file is optional, so Compose does not fail if it is missing. LOG_LEVEL is set directly in the Compose file, and a value in environment overrides the same name from env_file. API_TOKEN has no value, so Compose takes it from the shell where you run the command.
The files named in env_file configure the container. They are not read for interpolation.
Common mistake
A frequent error is putting a variable in .env and expecting the application to see it. If the app reports a missing variable, check whether the Compose file passes it through environment or env_file.
Exercise
Your .env file contains API_URL=https://api.example.com. The app in your web service cannot find API_URL, and your compose.yaml has no environment or env_file entries for web. Why, and what is the smallest fix?
Show answer
The .env file only supplies values for interpolation in the Compose file. It does not put variables into containers. Add environment: { API_URL: "${API_URL}" } to the web service. Compose interpolates the value from .env and sets it inside the container.