Sub topic 2
Setting Container Variables with environment
This chapter assumes you can write a basic compose.yaml, run docker compose up, and set shell variables. Here you will use the environment attribute to put variables into a container's process environment.
What environment does
The environment attribute of a service defines variables that exist inside the container when it starts. The program in the container reads them the usual way, for example with os.environ in Python or process.env in Node.js.
This is separate from interpolation. Interpolation is Compose replacing ${...} in the YAML file before anything runs. environment is what the container receives afterward. A single line can involve both.
Two syntaxes
You can write environment as a map or as a list. Both give the same result.
services:
app:
image: alpine:3.20
command: ["env"]
environment:
LOG_LEVEL: debug
MAX_WORKERS: 4
API_TOKEN:services:
app:
image: alpine:3.20
command: ["env"]
environment:
- LOG_LEVEL=debug
- MAX_WORKERS=4
- API_TOKENIn both files, API_TOKEN has no value. Compose then takes the value from the shell where you run docker compose. If the shell has no such variable, the container does not get it either. This keeps secrets out of the Compose file.
The map form is easier to read and merge. The list form uses NAME=value, so everything after the first = is the value.
Combining with interpolation
A value in environment can use interpolation, so Compose fills it in from the shell or the .env file:
services:
app:
image: alpine:3.20
command: ["env"]
environment:
LOG_LEVEL: ${LOG_LEVEL:-info}
DB_HOST: dbIf LOG_LEVEL is unset, the container receives info. The :- default syntax is covered in a later chapter.
Check what the container sees
Save the first example as compose.yaml and run it with a shell value for the token:
API_TOKEN=abc123 docker compose run --rm appThe output includes LOG_LEVEL=debug, MAX_WORKERS=4, and API_TOKEN=abc123. For a service that is already running, use docker compose exec app env. To see the Compose side without starting anything, run docker compose config.
Common pitfalls
- YAML turns some words into booleans. An unquoted
yes,on, ortruemay not reach the container as you typed it. Quote such values:DEBUG: "true". - Quotes in the list form are literal.
- NAME="x"gives the container the value"x"with the quote characters included. In the map form, YAML strips the quotes. - Dollar signs trigger interpolation. To pass a literal
$to the container, write$$. environmentbeatsenv_file. If a name appears in both, theenvironmentvalue wins.
Summary
Use environment for a small number of explicit container variables. Use the map form for readability. Leave the value out to pass a variable from the shell. Quote values YAML might reinterpret.
Exercise
A service has environment: with the list entry - GREETING="hello". The program prints "hello" with quote characters. Why does this happen, and how do you fix it?
Show answer
In the list form, everything after the first = is the literal value, so the quote characters are passed to the container. Fix it by writing - GREETING=hello, or by switching to the map form (GREETING: "hello"), where YAML removes the quotes.
Exercise
What does the container receive for API_TOKEN when the Compose file lists API_TOKEN: with no value, and the shell running Compose does not define API_TOKEN?
Show answer
Nothing. The variable is not set in the container, because there is no shell value to copy.