Sub topic 5
How Compose merges files
This chapter assumes you can read Compose YAML and have used -f or the automatic compose.override.yaml file from the previous chapters. Here you learn what happens when two files define the same service.
The basic idea
Compose reads files in order and merges each one into the result so far. The base file comes first. Later files win when they conflict. The merge works key by key inside each service, so you never copy a whole service into an override file.
How a key merges depends on its shape:
| Shape | Examples | Result |
|---|---|---|
| Single value | `image`, `build`, `restart`, `command`, `entrypoint` | The later value replaces the earlier one |
| Mapping | `environment`, `labels`, `depends_on` | Keys are combined. If both files set the same key, the later value wins |
| List | `ports`, `expose`, `dns` | Items are appended |
| List with a natural key | `volumes` | Items are combined by container path, and the later entry wins for the same path |
A worked example
Take the base and override files from earlier in this tutorial. The relevant web parts are:
# compose.yaml
services:
web:
image: example/web:1.0
ports:
- "8000:8000"
environment:
LOG_LEVEL: info# compose.override.yaml
services:
web:
build: .
volumes:
- ./src:/app/src
environment:
LOG_LEVEL: debug
ports:
- "5678:5678"The merged web service behaves as if you had written this:
services:
web:
image: example/web:1.0
build: .
ports:
- "8000:8000"
- "5678:5678"
volumes:
- ./src:/app/src
environment:
LOG_LEVEL: debugEach key follows its rule:
imagecomes only from the base file, andbuildcomes only from the override. Both survive, so Compose builds the image and tags it with theimagename.LOG_LEVELis a mapping key set in both files, so the override'sdebugwins.portsis a list, so the debugger port is added to port 8000. It does not replace it.
Lists that surprise people
Appending is the usual cause of confusing results. If the base file publishes "8000:8000" and your override publishes "9000:8000", you get both mappings. Two host ports now reach the same container port.
The same applies to command. It is a single value, so an override's command replaces the base one completely. Compose does not append arguments to it.
# compose.override.yaml
services:
web:
command: ["python", "manage.py", "runserver", "0.0.0.0:8000"]Environment in list form
The environment key accepts a mapping (LOG_LEVEL: debug) or a list (- LOG_LEVEL=debug). Compose converts both to the same form before merging. A list in one file and a mapping in another still merge key by key. Pick one style anyway, because it makes the files easier to read.
Relative paths
Compose resolves relative paths such as build: . and ./src against the directory of the first file you pass, not the directory of the file that contains them. If you keep files in a subfolder, check the paths in the merged output.
Check the result
Do not guess what a merge produced. Print it:
docker compose -f compose.yaml -f compose.override.yaml configThe output shows the final service definitions with variables filled in. Ports appear in their long form, with target and published fields, but the content is the same as above. The next chapter covers this command in detail.
Exercise
compose.yaml sets ports: ["8000:8000"] and environment: {LOG_LEVEL: info} for web. compose.prod.yaml sets ports: ["80:8000"] and environment: {LOG_LEVEL: warning}. You run docker compose -f compose.yaml -f compose.prod.yaml up. Which ports are published, and what is LOG_LEVEL?
Show answer
Both 8000:8000 and 80:8000 are published, because lists are appended. LOG_LEVEL is warning, because the later file wins for a shared mapping key.