Why
One compose.yaml in git serves both production and a test machine. The test machine needs a debugger port and
LOG_LEVEL=debug. Editing the shared file in place blocks git pull or slips into production on the next commit. The
override sits beside it, stays out of git (.gitignore), and changes only what it defines.
How the two files merge
# compose.yaml
services:
app:
image: ghcr.io/example-user/app:latest
ports: ["8080:80"]
environment:
LOG_LEVEL: info
TZ: Etc/UTC
# compose.override.yaml
services:
app:
ports: ["127.0.0.1:9229:9229"]
environment:
LOG_LEVEL: debug
app ends up with two ports (8080 and 9229), LOG_LEVEL: debug, and TZ: Etc/UTC. The merge rules:
| Field | Result |
|---|---|
scalar (image, restart) |
override wins |
mapping (environment, labels) |
keys merge; matching keys come from override |
ports |
lists are appended |
volumes |
matched by container path: an override volume on the same path replaces the base one, others stay |
Drop inherited fields: !reset and !override
Appending lists makes it impossible to close a port from the base file with plain YAML. Two tags handle that:
services:
app:
ports: !reset [] # no ports at all
# ports: !override ["127.0.0.1:8080:80"] — only this port
environment:
TZ: !reset null # remove one variable
Which files the stack reads
Without flags, Compose reads compose.yaml and, if present, compose.override.yaml. Passing -f compose.yaml reads
only the base file. COMPOSE_FILE in .env sets the file list explicitly — e.g.
COMPOSE_FILE=compose.yaml:compose.prod.yaml — and disables automatic override loading.
A separate case is pulling an entire external Compose file into the stack, such as a database from a sibling directory.
Use include: at the top of compose.yaml; relative paths inside the included file resolve against its own directory.
If it doesn’t work
You removed a port from compose.yaml, yet it's still open
- Cause
- The port comes from
compose.override.yaml: lists are appended, so override ports get added to the base ones. - Fix
- Check which files the stack reads with
docker compose ls, columnCONFIG FILES. Delete the stray override or remove the port from it.
You changed port 8080 to 127.0.0.1:8080:80 in the override, and up -d fails: failed to bind host port 127.0.0.1:8080/tcp: address already in use
- Cause
- The override didn't replace the port; it added a second one with the same number. Compose publishes both, and the second fails to bind.
- Fix
ports: !override ["127.0.0.1:8080:80"]replaces the base list.
You set COMPOSE_FILE in .env, and the override settings disappeared
- Cause
- With
COMPOSE_FILEset, Compose reads only the listed files and no longer picks up the override automatically. - Fix
- Add it to the list:
COMPOSE_FILE=compose.yaml:compose.override.yaml.
How to check
See which files the stack read and what they produced together:
docker compose ls
# NAME STATUS CONFIG FILES
# app running(1) /opt/app/compose.yaml,/opt/app/compose.override.yaml
docker compose config