Sub topic 5
How Compose merges files
This chapter assumes you can read Compose YAML, know what services, ports, environment variables, and volumes are, and have used docker compose up with more than one file. It covers the rules Compose applies when it combines files, so you can predict the result before you run anything.
The model: later files win, by shape
When Compose loads several files, it starts with the first one and merges each later file into it. The merge is done service by service, then key by key. What happens to a key depends on its shape: a single value, a mapping, or a list.
The base file and the override file used here are the ones from earlier chapters.
# compose.yaml (base)
services:
web:
image: example/web:1.0
ports:
- "8000:8000"
environment:
LOG_LEVEL: info
depends_on:
- db
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD}
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:# compose.override.yaml (loaded automatically after compose.yaml)
services:
web:
build: .
volumes:
- ./src:/app/src
environment:
LOG_LEVEL: debug
ports:
- "5678:5678"Compose treats each key in the override like this:
| Shape | Examples | Rule |
|---|---|---|
| Scalar (single value) | `image`, `command`, `restart`, `user` | The later file replaces the earlier value. |
| Mapping (key/value pairs) | `environment` in map form, `labels`, `build` | Keys are merged. Same key: the later value wins. New key: it is added. |
| Sequence (list) | `ports`, `volumes`, `expose`, `dns` | Items from both files are combined. Some lists are de-duplicated by a key. |
| Command-like lists | `command`, `entrypoint` | The later file replaces the whole list. It is never appended. |
The last row matters. command and entrypoint are lists in YAML, but Compose treats them as one unit. Appending extra arguments to a start command would almost never produce a working command.
Scalars: the last value wins
In the example, web has image: example/web:1.0 in the base and build: . in the override. These are different keys, so both are kept. The merged service has an image name and a build section. With both present, docker compose build builds the local source and tags it with the image name. docker compose up uses the image if it exists and builds it otherwise (unless you pass --build).
If the override had also set image: example/web:dev, the base value would be dropped, because both files set the same scalar key.
Mappings: merged key by key
environment accepts a mapping (LOG_LEVEL: info) or a list of KEY=value strings. Compose normalizes both forms to a mapping before it merges, so you can mix forms across files.
In the example, the base sets LOG_LEVEL: info and the override sets LOG_LEVEL: debug. The merged result has LOG_LEVEL: debug. If the base also had TZ: UTC, it would survive, because the override does not mention TZ.
# base
services:
web:
environment:
LOG_LEVEL: info
TZ: UTC
labels:
team: platform# override
services:
web:
environment:
- LOG_LEVEL=debug
- FEATURE_X=on# merged result (conceptually)
services:
web:
environment:
LOG_LEVEL: debug
TZ: UTC
FEATURE_X: "on"
labels:
team: platformNote that the merged form quotes "on". Compose prints values as strings in its normalized output, so check docker compose config rather than assuming how a value will look.
Lists: appended, with some keys that collapse
For ports, the override adds to the base. The merged web service publishes both 8000:8000 and 5678:5678. This is what you want when development needs an extra debugger port, and it is a trap when you want to change a port instead. Overriding "8000:8000" with "9000:8000" gives you both mappings, not a replacement.
Some list entries are matched by a key, and a later entry with the same key replaces the earlier one instead of adding a duplicate:
| List | Entries are matched by |
|---|---|
| `volumes` | The container path (the mount target) |
| `ports` | The container port and protocol, plus host settings for the entry |
| `secrets`, `configs` | The source name |
| `extra_hosts`, `tmpfs`, `devices` | The target or full value |
This is why a base volume ./data:/app/data is replaced by an override volume ./dev-data:/app/data. Both mount to /app/data, so only the later one remains. Two entries with different container paths are both kept.
depends_on can be a list or a mapping. A list is normalized into a mapping, and mappings merge by service name. So an override that adds condition: service_healthy for db changes that one dependency and leaves the rest alone.
Relative paths are resolved from the first file
Paths such as build: . or ./src:/app/src are resolved relative to the directory of the first Compose file you pass, not the directory of the file that contains them. If you keep override files in a subdirectory like deploy/compose.prod.yaml, a ./src path inside it still points at the project directory of the base file. Use --project-directory if you need a different anchor.
The merge order in practice
Order on the command line is the order of the merge. The last file has the final say.
# Development: compose.yaml + compose.override.yaml are used automatically
docker compose up -d
# Production: -f disables the automatic override file, so list every file you want
docker compose -f compose.yaml -f compose.prod.yaml up -d
# The same selection through an environment variable (default separator is : on Linux and macOS)
COMPOSE_FILE=compose.yaml:compose.prod.yaml docker compose up -dThe production file from the earlier example only sets scalars and a mapping value:
# compose.prod.yaml
services:
web:
restart: unless-stopped
environment:
LOG_LEVEL: warning
db:
restart: unless-stoppedMerged on top of the base, web ends up with restart: unless-stopped and LOG_LEVEL: warning. It keeps its image, its port 8000:8000, and its depends_on entry.
Replacing and removing with tags
Default rules cannot express "replace this whole list" or "delete this inherited key". Recent Compose versions add two YAML tags for this:
# compose.override.yaml: merge behavior in one place
services:
web:
command: ["python", "manage.py", "runserver", "0.0.0.0:8000"]
ports: !override
- "9000:8000"
environment:
DEBUG: !reset null!overridediscards the inherited value and uses only what follows. Herewebpublishes only9000:8000.!reset nullremoves the key. HereDEBUGis dropped from the environment even if the base sets it.
These tags depend on your Compose version. If your version does not recognize them, the file fails to load. Check with docker compose version before you rely on them, and see the dedicated chapter on them for details.
Verify the result instead of guessing
docker compose config prints the final merged and interpolated configuration without starting anything. Use it with the same file arguments you will use for the real run.
# Print the fully merged and interpolated configuration without starting anything
docker compose -f compose.yaml -f compose.prod.yaml configBecause the base requires DB_PASSWORD, this command fails with set DB_PASSWORD until you export the variable. That is the intended behavior of the ${DB_PASSWORD:?...} form, and it also means a missing secret is caught before any container starts.
Common mistakes
- Expecting a port to be replaced. Ports are appended. Use
!overrideor restructure so only one file defines the port. - Forgetting that
-fdisables the automatic override.docker compose -f compose.yaml updoes not readcompose.override.yaml. If you want it, list it. - Wrong file order.
-f compose.prod.yaml -f compose.yamllets the base overwrite production settings. Base first, specific files after. - Mixing
buildin one file andimagein another and assuming one wins. They are different keys, so both are kept. - Expecting
commandarguments to be added. The latercommandreplaces the earlier one completely.
Exercise
A base file defines web with ports: ["8000:8000"] and environment: {LOG_LEVEL: info, TZ: UTC}. An override file defines web with ports: ["9000:8000"] and environment: {LOG_LEVEL: debug}. What ports and environment does the merged web service have, and how would you change the override so that only port 9000 is published?
Show answer
The merged service publishes both 8000:8000 and 9000:8000, because lists such as ports are appended and these two entries differ in host port. The environment is a mapping, so it is merged by key: LOG_LEVEL is debug (the later value wins) and TZ stays UTC. To publish only 9000, write ports: !override followed by the list - "9000:8000" in the override file, on a Compose version that supports the tag.
Exercise
You run docker compose -f compose.yaml up -d in a project that has a compose.override.yaml. Which files does Compose load, and which command prints the result of merging so you can confirm it?
Show answer
Only compose.yaml is loaded, because passing -f disables the automatic loading of compose.override.yaml. To see the merged result for the files you actually use, run docker compose -f compose.yaml config, adding every -f file you intend to use, in merge order.