Why Compose uses variables
The Compose file lives in git and stays identical across servers. Values that are per-server or secret — data
directory, domain, passwords — live in .env next to it, only on the server; git holds .env.example with the names
(Server configs in git). Before starting, Compose reads .env and fills in ${…} placeholders
(.env feeds compose substitution, env_file goes into the container).
What happens when a line in .env is missing
On a new server you clone the repo, fill out .env from the example, and miss one line. In the Compose file:
services:
db:
image: mariadb:${MARIADB_TAG:-11.8}
volumes:
- ${DATA_DIR}/db:/var/lib/mysql
Compose doesn’t stop. It prints one warning line — The "DATA_DIR" variable is not set. Defaulting to a blank string.
— and starts the stack. The host path becomes /db at the root of the disk: Docker creates it on its own
(A missing volume path), the database writes there, and no error shows up. Once someone adds the missing line to
.env, the stack starts with an empty directory at the right path while the data stays behind in /db.
Stopping Compose with a hint
Add :? and a message after the variable name:
- ${DATA_DIR:?DATA_DIR in .env — stack data directory, e.g. /opt/app/data}/db:/var/lib/mysql
Without DATA_DIR, Compose creates nothing and prints that message:
docker compose up -d
# error while interpolating services.db.volumes.[]: required variable DATA_DIR is missing a value: DATA_DIR in .env — stack data directory, e.g. /opt/app/data
The text after ? reads like a comment because it is one. Compose prints it only when the variable is missing — to
whoever brings the stack up on a new server or a year from now. State where to set the variable and what a valid value
looks like. An empty DATA_DIR= in .env stops Compose the same way.
When to use :? and when to use :-
Use :? wherever the stack makes no sense without a value and there’s no safe guess: data directory, domain, image
name, password. Where a default works, use :-: ${MARIADB_TAG:-11.8} falls back to 11.8 when unset. Values
hardcoded in Compose without ${…} don’t need a check — this syntax is for values moved into .env.
All interpolation forms
In your own files, :? and :- are enough. Ready-made Compose files from project READMEs use all six forms — keep
this table as a reference. The colon means an empty value counts as unset.
| Syntax | Unset | X= (empty) |
X=v |
|---|---|---|---|
${X:-d} |
d |
d |
v |
${X-d} |
d |
empty | v |
${X:?text} |
error with text | error with text | v |
${X?text} |
error with text | empty | v |
${X:+a} |
empty | empty | a |
${X+a} |
empty | a |
a |
:+ flips the logic: the value appears only when the variable is set.
Interpolations can be nested. Here the service domain comes from APP_HOST, falling back to app. plus the shared
stack domain; Compose checks DOMAIN only when APP_HOST isn’t set:
labels:
- "traefik.http.routers.app.rule=Host(`${APP_HOST:-app.${DOMAIN:?DOMAIN in .env — example.org}}`)"
Inside .env itself you can reference an earlier variable: IMAGE=${REGISTRY}/app. Values in single quotes aren’t
expanded — that’s how password hashes with $ live in .env without breaking (The $ sign).
If it doesn’t work
up -d fails: unable to get image ':latest', invalid reference format
- Cause
- The Compose file has
image: ${IMAGE}:…without a check, and the variable isn't set. Compose substituted an empty string and passed Docker a nameless image.${IMAGE?…}without a colon does the same if.envhas an emptyIMAGE=. - Fix
- Write
${IMAGE:?…}so Compose stops with a hint before talking to Docker.
.env has MARIADB_TAG=11.8, yet docker compose config shows a different tag
- Cause
- A variable with the same name exists in the shell environment — from
~/.bashrc, a script, orexportin this session. The environment overrides.env, even when empty:MARIADB_TAG=in the environment triggers the:-default. - Fix
- Find it with
env | grep MARIADB_TAG, clear it withunset MARIADB_TAG, and remove the line in the file it came from.
You removed a variable from .env — now ps, logs, and even down fail while containers keep running
- Cause
- Compose interpolates the whole file before running any command, and
:?stops every one of them.docker compose lsstill works because it doesn't read the file. - Fix
- Put the line back in
.env. To stop the stack without it, rundocker compose -p <name> down(get the name fromdocker compose ls). With-p, Compose finds containers by project labels.
How to check
config --variables lists every variable the Compose file expects, even before .env exists:
docker compose config --variables
NAME REQUIRED DEFAULT VALUE ALTERNATE VALUE
MARIADB_TAG false 11.8
DATA_DIR true
REQUIRED true marks what must be in .env. Check what got substituted in the rendered config:
docker compose config | grep source:
# source: /opt/app/data/db