Sub topic 6
Checking the merged result with docker compose config
This chapter assumes you can read Compose YAML, know what -f and compose.override.yaml do, and have a base file plus an override or production file to test against.
When several files are merged, you can't tell the result by reading each file alone. docker compose config prints the final configuration after merging and variable interpolation, and it starts no containers. Run it before you start a stack and whenever an environment behaves unexpectedly.
What the command does
docker compose config goes through the same steps as up, then stops and prints the result:
- It selects the files:
-fflags,COMPOSE_FILE, or the defaultcompose.yamlpluscompose.override.yaml. - It merges them in order, with later files winning.
- It substitutes variables from the shell and from
.env. - It validates the result and prints normalized YAML.
Comparing environments
Use the same -f list you would pass to up:
# Development: the default files are used automatically
DB_PASSWORD=devpass docker compose config
# Production: list every file you want, because -f disables the automatic override
DB_PASSWORD=prodpass docker compose -f compose.yaml -f compose.prod.yaml configWith the files from earlier chapters, the production output differs from development in several ways:
webhasrestart: unless-stopped.LOG_LEVELiswarning, notdebug.- There is no
buildkey and no./srcbind mount. - Port
5678is absent.
If you expected a difference and it is missing, the file was probably not loaded or the merge rule worked differently than you assumed.
Reading the output
Compose normalizes the configuration, so the output does not match your source text exactly:
- Short port syntax such as
"8000:8000"becomes the long form withmode,target,published, andprotocol. - Relative paths become absolute paths.
- Variables are replaced by their values.
- Environment lists may be rewritten as mappings.
Those changes are expected. Look at the values, not the formatting.
A missing required variable also shows up here. The base file uses ${DB_PASSWORD:?set DB_PASSWORD}. If DB_PASSWORD is unset, config fails with the message set DB_PASSWORD, so it catches the problem before anything starts.
Useful options
# Print the final merged configuration to stdout
docker compose -f compose.yaml -f compose.prod.yaml config
# Validate only: print nothing, return a non-zero exit code on error
docker compose -f compose.yaml -f compose.prod.yaml config --quiet
# List the services that would start with a given profile
docker compose --profile debug config --services
# Write the merged result to a file, for example to review or diff it
docker compose -f compose.yaml -f compose.prod.yaml config --output merged-prod.yaml--services is the quickest way to check profiles. Without --profile, it lists only web and db. With --profile debug, it also lists adminer.
Diffing two environments
Redirecting the output of two runs shows exactly how the environments differ:
export DB_PASSWORD=example
docker compose config > dev.yaml
docker compose -f compose.yaml -f compose.prod.yaml config > prod.yaml
diff dev.yaml prod.yamlBecause both files are normalized, the diff contains real differences and not formatting noise.
Common mistakes
- Running
configwithout the same-fflags orCOMPOSE_FILEasup. You then inspect a different configuration than the one that runs. - Forgetting that
-fdisables the automatic override. If development settings appear in a production check, you probably listedcompose.override.yamlyourself or leftCOMPOSE_FILEset in your shell. - Having a different
.envor shell variable set. The same files can print different results on different machines.
Exercise
You run docker compose -f compose.yaml -f compose.prod.yaml config and still see LOG_LEVEL: debug. Name two things you would check, and give a command that confirms the active file selection.
Show answer
Check whether compose.override.yaml is being loaded, for example through a COMPOSE_FILE variable left in the shell, and check whether the order of the -f flags puts a debug-setting file after compose.prod.yaml, since later files win. Run docker compose -f compose.yaml -f compose.prod.yaml config after running env | grep COMPOSE to see whether COMPOSE_FILE is set. The -f flags take precedence over COMPOSE_FILE, so a stale COMPOSE_FILE is not the cause here, and the environment section of the output shows which value won.