Sub topic 6
Defaults and required variables
This chapter assumes you can read a compose.yaml, know that Compose replaces ${VAR} expressions before it starts anything, and can set shell variables. Here you will learn the operators that give a variable a fallback or make it mandatory.
Why a plain `${VAR}` is not enough
If a variable is unset, Compose substitutes an empty string and prints a warning. The service then starts with a blank value, such as an image tag of postgres:. That error appears late and is hard to trace. Defaults and required checks move the failure to the moment you run the command.
The operators
Compose supports a set of operators inside ${...}. They differ in how they treat a variable that is unset versus set but empty.
| Syntax | Result |
|---|---|
| `${VAR:-default}` | `default` if `VAR` is unset or empty |
| `${VAR-default}` | `default` only if `VAR` is unset |
| `${VAR:?message}` | Error with `message` if `VAR` is unset or empty |
| `${VAR?message}` | Error with `message` only if `VAR` is unset |
| `${VAR:+replacement}` | `replacement` if `VAR` is set and not empty, otherwise empty |
| `${VAR+replacement}` | `replacement` if `VAR` is set, even if empty, otherwise empty |
The colon versions treat an empty value like an unset one. The versions without a colon check only whether the variable exists.
A worked example
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"POSTGRES_TAGis optional. Without it, the image ispostgres:16.DB_PORTis optional. Without it, the host port is5432.POSTGRES_PASSWORDis mandatory. If it is unset or empty, Compose stops and prints your message.
Values can come from the shell or from a .env file next to compose.yaml:
POSTGRES_USER=app
POSTGRES_PASSWORD=change-me
DB_PORT=5432Remove the password line and run the check:
docker compose configCompose exits with an error that includes POSTGRES_PASSWORD must be set, and it starts nothing.
Choosing between `:-` and `-`
The two forms differ when a variable is present but empty, for example DB_PORT= in .env.
${DB_PORT:-5432}gives5432, because the empty value counts as missing.${DB_PORT-5432}gives an empty string, because the variable is set.
Use the colon form in most cases. A blank value is rarely what you want, and it often comes from a line like DB_PORT= that someone forgot to fill in.
Using `:+` for optional pieces
The :+ operator produces text only when a variable has a value. This is useful for building an optional fragment:
services:
app:
image: my-app:latest
environment:
EXTRA_FLAGS: ${DEBUG:+--verbose}With DEBUG=1, the container receives EXTRA_FLAGS=--verbose. With DEBUG unset, EXTRA_FLAGS is empty.
Common mistakes
- Putting defaults in the wrong place. These operators work only in the Compose file. A
.envfile does not interpret${VAR:-x}for its own purposes the way the Compose file does, so keep fallbacks incompose.yaml. - Forgetting the colon.
${VAR?msg}accepts an empty value and lets a blank password through. - Vague messages. Write
:?POSTGRES_PASSWORD must be set, not:?missing. The message is what a teammate sees.
Exercise
A compose.yaml contains image: web:${TAG-latest}, and .env contains the line TAG=. What image does Compose use? Which operator would make it use web:latest?
Show answer
The image is web:, because TAG is set (to an empty string) and - only applies the default when the variable is unset. Using ${TAG:-latest} treats the empty value as missing and gives web:latest.