Sub topic 7
Checking the Result with docker compose config
This chapter assumes you can read a basic compose.yaml, set shell variables, and run docker compose up. It also assumes you know that Compose can fill in ${VAR} expressions from a .env file or the shell.
Why check before you run
Interpolation happens before Compose creates anything. If a variable is blank or comes from the wrong source, you often find out only after a container starts with a wrong port or password. docker compose config removes that guesswork. It reads your Compose file, resolves every variable, merges any extra files, and prints the final result. It does not start containers.
A first run
Take this Compose file, with a .env file next to it that contains DB_PORT=5432:
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"Run the check from the project directory:
DB_PORT=5433 docker compose configThe output shows the model Compose would use. The image is postgres:16, because POSTGRES_TAG is unset and the default applies. The published port is 5433. The shell value beat the .env value, which is the precedence rule covered in the next chapter.
If POSTGRES_PASSWORD is unset or empty, the command stops with the message you wrote after :?. That is the intended behavior. A failing config is a cheap way to catch a missing required value.
What to look for
Read the output with these questions in mind:
- Does each
${...}expression show the value you expected? - Did a variable you expected silently become an empty string?
- Do
ports,image, andenvironmentmatch your intent? - Did a
$$escape appear as a single$?
Compose prints a warning when it finds a variable that is not set and has no default. Treat that warning as a prompt to look at the corresponding line in the output.
Useful options
| Command | Effect |
|---|---|
| `docker compose config` | Prints the fully resolved Compose model |
| `docker compose --env-file .env.staging config` | Uses a different file as the source of interpolation values |
| `docker compose config --quiet` | Prints nothing and only reports errors through the exit code |
| `docker compose config --variables` | Lists the variables the file uses, with their defaults and whether they are required |
| `docker compose config --environment` | Prints the variables Compose itself sees as it resolves the file |
The --quiet option is handy in scripts and continuous integration. A nonzero exit code means the file is invalid or a required variable is missing. Options can differ between Compose versions, so run docker compose config --help to see what yours supports.
Limits of the check
config shows the result of interpolation and file merging. It does not show variables baked into an image with ENV. It also does not show what a running process sees after the container's own shell expands values. For that, start the container and inspect it, for example with docker compose run --rm app env.
Exercise
You run docker compose config and the output shows ports: - "5432:5432", but you expected 5433 because .env contains DB_PORT=5433. The Compose file uses "${DB_PORT:-5432}:5432". Name two likely causes and a command that helps you find them.
Show answer
Either the .env file is not in the project directory (so Compose did not read it and used the default), or the value is being read from another source, such as a DB_PORT=5432 already set in the shell, or a different file chosen with --env-file. Run docker compose config from the correct directory, and use docker compose config --environment to see which variables Compose resolved, or check env | grep DB_PORT in the shell.