Sub topic 14
Troubleshooting common surprises
This chapter assumes you have read the earlier chapters. You should know how compose.override.yaml is loaded, how -f and COMPOSE_FILE select files, how Compose merges values, and how profiles and variable interpolation work. Here you learn to find out why an environment does not behave as you expected.
Start with the merged result
Most surprises come from a gap between the files you wrote and the configuration Compose actually used. Check the actual configuration before you edit anything.
Use these commands as your first step every time:
# The merged, interpolated configuration for the files and profiles you would use
docker compose config
# Which files were loaded, and which services would start
docker compose config --services
# The same, with a profile active
docker compose --profile debug config --services
# Does the output reflect your shell or an env file? Show variables Compose resolved
docker compose config --environment--environment is available only in recent Compose releases. If your version rejects it, run docker compose version and update.
Surprise 1: the override file disappeared
You ran docker compose -f compose.yaml -f compose.prod.yaml up -d and expected development settings to be absent. They are. The reverse mistake is more common: you ran docker compose -f compose.prod.yaml up -d and the base file was missing, so db was undefined.
Remember the rule from the earlier chapters. With no -f and no COMPOSE_FILE, Compose loads compose.yaml and then compose.override.yaml. As soon as you pass -f or set COMPOSE_FILE, it loads only the files you list. List the base file explicitly every time.
A stale COMPOSE_FILE causes the opposite problem. If a previous session exported it, a plain docker compose up silently uses that selection and ignores the automatic override file. Check with:
echo "$COMPOSE_FILE"
docker compose config | head -n 20On Windows, the default separator in COMPOSE_FILE is ;, not :. Set COMPOSE_PATH_SEPARATOR if you need another character.
Surprise 2: a list grew instead of being replaced
Given the base file, this override looks like it changes the web port:
# compose.override.yaml
services:
web:
ports:
- "9000:8000"The base file already publishes 8000:8000. Merging appends list items for ports, so the final service publishes both 8000 and 9000. Run docker compose config and you will see both entries.
The merge behavior differs by attribute type:
| Attribute type | Examples | Merge behavior |
|---|---|---|
| Single value | `image`, `restart`, `command` | Later file replaces earlier value |
| Mapping | `environment`, `labels` | Merged key by key, later file wins on conflict |
| List of entries | `ports`, `expose`, `dns` | Entries are appended |
| Keyed list | `volumes` | Merged by container path, later file wins for the same target |
To replace a whole list, use !override. To remove an inherited value, use !reset:
# compose.override.yaml
services:
web:
ports: !override
- "9000:8000"
environment:
DEBUG: !reset nullIf Compose reports an error about an unknown tag, your Compose release is too old for these tags. Upgrade it, or restructure the files so the base file does not define the value at all. Moving environment-specific values out of the base file is often the cleaner fix.
Surprise 3: a service never starts
You added profiles: [debug] to adminer, then ran docker compose up -d, and nothing happened for it. This is correct behavior. A service with a profiles attribute starts only when one of its profiles is active. Check the cause with:
docker compose config --services
docker compose --profile debug config --servicesThe first list lacks adminer; the second includes it. Three ways to activate it are --profile debug, COMPOSE_PROFILES=debug, and naming the service on the command line (docker compose up adminer or docker compose run --rm migrate).
A related error appears when a default service depends on a profiled one:
services:
web:
image: example/web:1.0
depends_on:
- adminer # adminer has profiles: [debug]
adminer:
image: adminer
profiles: [debug]Without the debug profile active, Compose cannot start web, because its dependency is not part of the project. Dependencies should point from a profiled service to an unprofiled one (as migrate depends on db), not the other way around. If the dependency must be optional, give both services the same profile.
Surprise 4: `down` leaves containers running
You started the stack with --profile debug, then ran docker compose down. The adminer container is still running, and the network may refuse to be removed because it is in use.
down also respects profiles. Without the profile active, Compose does not consider adminer part of the project for that command. Pass the same profile to down:
docker compose --profile debug downRecent Compose releases also accept a wildcard that activates every profile, which is useful for cleanup:
docker compose --profile "*" downThe same reasoning explains why docker compose ps may not show profiled containers. Add --profile or use docker compose ps -a with the profile active.
Surprise 5: variables are empty, wrong, or an error
The base file in this tutorial uses ${DB_PASSWORD:?set DB_PASSWORD}. If DB_PASSWORD is unset or empty, any command that interpolates the file fails with your message. That is intentional. Supply the value from your shell or from an env file.
Know the precedence so you can predict which value wins. From highest to lowest, Compose uses:
- Values set with
docker compose run -efor that run. - Variables exported in your shell.
- Values from
--env-file, or from.envin the project directory when you do not pass--env-file. - Defaults written in the file, such as
${LOG_LEVEL:-info}.
A shell variable beats .env. This explains a classic puzzle: you edit .env, but the old value remains because an earlier export is still in your shell. Check with docker compose config, then run env | grep DB_ to find it.
Three more traps:
.envis read from the project directory, which is the directory of the first-ffile. If you rundocker compose -f deploy/compose.yaml ...from the repository root,.envin the root is not read. Use--env-file .envor--project-directory ..--env-filefor interpolation is different from theenv_file:service attribute. The first fills${...}placeholders in the Compose file. The second passes variables into the container.- A literal dollar sign in a command or password must be written
$$. A single$starts interpolation.
Surprise 6: relative paths point to the wrong place
Compose resolves relative paths in every file against the project directory, not against each file's own location. If you keep compose.prod.yaml in deploy/ and write ./src there, Compose looks for src relative to the first file's directory. Run docker compose config and read the resolved absolute paths under volumes and build.context. Fix the path or set --project-directory.
Quick reference
| Symptom | Likely cause | Check | Column |
|---|---|---|---|
| Dev settings missing | `-f` or `COMPOSE_FILE` bypassed the override file | `docker compose config` | |
| Two ports published | List merge appended | Use `!override` | |
| Service never starts | Inactive profile | `config --services` with `--profile` | |
| `down` leaves containers | Profile not active for `down` | `--profile "*" down` | |
| Required variable error | Variable unset, or `.env` not read | `env`, `--env-file` | |
| Old variable value | Shell export beats `.env` | `env | grep NAME` |
A worked diagnosis
A teammate reports that docker compose --profile debug up -d fails with "required variable DBPASSWORD is missing". They did create .env with `DBPASSWORD=secret`. Walk through the steps:
cd ~/project
pwd
ls -a # is .env present here?
env | grep DB_PASSWORD # does the shell set it to an empty value?
docker compose config # does it fail the same way?
docker compose --env-file ./.env config | grep POSTGRES_PASSWORDIf the last command works, the project directory differs from where .env lives, or the shell holds an empty DB_PASSWORD that overrides the file. An empty shell variable counts as set, but :? rejects empty values, so the check still fails. Run unset DB_PASSWORD to fix it.
Exercise
You run docker compose -f compose.yaml -f compose.prod.yaml up -d and later docker compose down. A container from the adminer service (profile debug) that you started earlier is still running. Why, and what command fixes it?
Show answer
down only acts on services whose profiles are active, and no profile was passed, so adminer was not included. Run docker compose --profile debug down, or docker compose --profile "*" down on releases that support the wildcard. Add the same -f files too if the services depend on them.
Exercise
The base file publishes "8000:8000" for web. Your override file sets ports: ["9000:8000"], and now both ports are open. Give two ways to publish only 9000.
Show answer
Mark the override list with !override (ports: !override followed by the 9000:8000 entry), which replaces the inherited list. Alternatively, remove the ports entry from the base file and define ports only in the environment-specific files. Confirm either result with docker compose config.