Sub topic 6
Checking the merged result with docker compose config
This chapter assumes you can run a multi-service app with docker compose up, read YAML, and set shell environment variables. It also assumes you have seen compose.yaml and compose.override.yaml combined, as in the previous chapters. Here you will learn to look at the configuration Compose actually uses, before you start any containers.
What the command does
docker compose config reads your Compose files, applies the selected profiles, substitutes variables, merges everything, and prints the result. It starts no containers and creates no networks or volumes. It only prints.
That makes it the first tool to reach for when an environment behaves unexpectedly. Reading three YAML files and working out the merge in your head is error-prone. Asking Compose is not.
docker compose configWith no flags, it uses the same file selection as docker compose up. In the example project, that means compose.yaml plus compose.override.yaml. To check production, pass the same -f flags (or COMPOSE_FILE) you will use for the real run:
DB_PASSWORD=example docker compose -f compose.yaml -f compose.prod.yaml configIf the file selection differs between your check and your deploy command, the check proves nothing. Treat the two commands as a pair.
Reading the output
Take the base file and development override from this tutorial's project. Run DB_PASSWORD=example docker compose config in that directory. The output looks like this (abridged, and details vary by Compose version):
name: myapp
services:
db:
environment:
POSTGRES_PASSWORD: example
image: postgres:16
networks:
default: null
volumes:
- type: volume
source: db-data
target: /var/lib/postgresql/data
volume: {}
web:
build:
context: /home/you/myapp
dockerfile: Dockerfile
depends_on:
db:
condition: service_started
required: true
environment:
LOG_LEVEL: debug
image: example/web:1.0
networks:
default: null
ports:
- mode: ingress
target: 8000
published: "8000"
protocol: tcp
- mode: ingress
target: 5678
published: "5678"
protocol: tcp
volumes:
- type: bind
source: /home/you/myapp/src
target: /app/src
bind:
create_host_path: true
networks:
default:
name: myapp_default
volumes:
db-data:
name: myapp_db-dataSeveral things in this output are worth noticing.
LOG_LEVELisdebug. The override replaced the base valueinfo. This is the scalar rule from the merge chapter, confirmed in the result.- Both ports appear.
8000comes from the base file and5678from the override. Port lists are appended, not replaced. - Both
imageandbuildare present onweb. The base file supplied the image name and the override supplied a build. Compose will build locally and tag the result with that name. - Short syntax is expanded.
"8000:8000"became a mapping withtarget,published, andprotocol.depends_on: [db]became the long form withcondition: service_started. - Relative paths are absolute.
./srcand the build context now point to real directories on your machine. This is why the output from one machine will not match another's byte for byte. - Compose added defaults. The project
name, thedefaultnetwork, and the prefixed volume name were not in your files.
Because of this normalization, do not copy the output back into your source files. It is a diagnostic view, not an authoring format.
Narrowing the output
The full output is long. Several flags print only the part you need.
| Flag | What it prints |
|---|---|
| `--services` | Names of services that would be started, one per line |
| `--profiles` | Names of all profiles defined in the files |
| `--images` | Image names used by the selected services |
| `--volumes` | Names of named volumes |
| `--variables` | Variables the files reference, with defaults and whether they are required |
| `--format json` | The same configuration as JSON, for scripts |
| `--quiet` or `-q` | Nothing; only the exit code reports success or failure |
| `--output FILE` | Writes the result to a file instead of the terminal |
| `--no-interpolate` | Leaves `${VAR}` expressions unsubstituted |
Some flags, such as --variables and --environment, exist only in newer Compose releases. Run docker compose config --help to see what your installation supports.
The most useful pair when debugging profiles is:
docker compose config --profiles
docker compose --profile debug config --servicesThe first lists every profile the files define, so you can spot a typo such as degub. The second shows which services would start with debug active.
Checking profiles
Services with a profiles attribute are left out of the output unless one of their profiles is active. This is the quickest way to test your profile setup.
With the adminer and migrate services from the profiles chapter added to the base file, compare these commands:
DB_PASSWORD=example docker compose config --services
DB_PASSWORD=example docker compose --profile debug config --services
DB_PASSWORD=example COMPOSE_PROFILES=debug,tools docker compose config --servicesThe first prints db and web. The second adds adminer. The third adds adminer and migrate. If a service you expect is missing, no active profile matches it.
Naming a service on the command line also activates its profiles for run and up. config does not take a service argument for this purpose, so use --profile or COMPOSE_PROFILES when checking.
Checking interpolation
The base file contains ${DB_PASSWORD:?set DB_PASSWORD}. The :? form makes Compose stop with your message if the variable is unset or empty. config evaluates it too, so a missing variable shows up as an error, with no container started:
docker compose config -qWithout DB_PASSWORD, this exits with a non-zero status and prints an error containing set DB_PASSWORD. With it set, the command exits with status 0 and prints nothing. That makes config -q a cheap validation step for scripts and CI.
To see which values came from where, use --no-interpolate:
docker compose config --no-interpolateThe output keeps the ${DB_PASSWORD:?set DB_PASSWORD} text in place. Comparing it with the normal output shows exactly which fields depend on variables.
A verification routine
A short routine catches most mistakes before they reach a running system.
- Run
config -qwith the exact files and variables of the target environment. It must exit with status 0. - Run
config --serviceswith the target profiles. Compare the list with what you expect. - Run the full
configand inspect the fields you changed:image,ports,environment,volumes,restart. - Confirm that nothing development-only leaked in. For production, search the output for bind mounts and debugger ports:
DB_PASSWORD=example docker compose -f compose.yaml -f compose.prod.yaml config \
| grep -E "type: bind|5678" || echo "no dev-only settings found"Because the production file list omits compose.override.yaml, this should print the "no dev-only settings found" message. If it finds a match, the override file is being loaded by some other route, for example a COMPOSE_FILE value left in your shell.
Diffing two environments
Since the output is deterministic text, you can compare environments directly:
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.yamlThe diff should show only the intended differences: the build and bind mount, the log level, the debugger port, and the restart policies. Anything else is a surprise worth investigating. Delete these files afterward, because they contain the password.
Common mistakes when checking
- Checking the wrong file set. Running plain
docker compose configincludes the automatic override file, while your production command does not. Always repeat the real-flist. - A stale
COMPOSE_FILEorCOMPOSE_PROFILESin your shell. These change the result silently. Runenv | grep COMPOSE_when output looks wrong. - Expecting
configto prove the app works. It validates structure and shows the merge. It does not check that images exist, that ports are free, or that the application starts. - Reading a merged list as if it were replaced. Ports, volumes, and similar lists combine. If you want only the override's list, use the
!overridetag from the later chapter and confirm the result here.
Exercise
Your production deploy runs docker compose -f compose.yaml -f compose.prod.yaml up -d, but a bind mount of ./src appears in the running web container. Which commands would you run to find the cause, and what would you look for?
Show answer
Run docker compose -f compose.yaml -f compose.prod.yaml config and check whether web has a type: bind volume. If it does, a file other than the two listed is being merged in, or the files themselves contain the mount. Next, run env | grep COMPOSE_ to look for a leftover COMPOSE_FILE that adds compose.override.yaml. Also check that compose.yaml itself has no bind mount. If config shows no bind mount, the running container came from an earlier up with different files, so recreate it with docker compose -f compose.yaml -f compose.prod.yaml up -d --force-recreate.
Exercise
You add a service mailhog with profiles: [debug]. docker compose config --services does not list it. Is that a bug, and how do you list it?
Show answer
It is expected behavior. A service with a profiles attribute is included only when one of its profiles is active. Run docker compose --profile debug config --services, or set COMPOSE_PROFILES=debug, and mailhog appears. To confirm the profile name is spelled correctly, run docker compose config --profiles.