Sub topic 12
Override files, multiple -f files, and include
This chapter assumes you can read a compose.yaml, run docker compose up, and know what services, ports, and volumes are. It covers three ways to split one stack's configuration across several files.
Automatic override files
When you run docker compose with no -f flag, Compose reads compose.yaml and then merges compose.override.yaml from the same directory, if it exists. Use the override file for settings that only apply on your machine, such as a debug flag or a bind mount of your source code.
# compose.override.yaml (merged automatically with compose.yaml)
services:
web:
environment:
DEBUG: "1"
volumes:
- ./src:/app/srcRun docker compose config to see the merged result before you start anything.
Choosing files with -f
The -f flag selects files explicitly. Files are merged in the order given, and later files win. Passing -f turns off the automatic override, so list every file you want.
docker compose -f compose.yaml -f compose.prod.yaml config
docker compose -f compose.yaml -f compose.prod.yaml up -dA common layout is a base file plus one file per environment: compose.yaml, compose.dev.yaml, compose.prod.yaml.
How merging works
Compose merges file by file, matching services by name. The rules depend on the kind of value.
| Kind of value | Examples | Merge result |
|---|---|---|
| Single value | `image`, `command`, `build.context` | The later file replaces the earlier one |
| Mapping | `environment`, `labels` | Keys are combined, and the later file wins on a shared key |
| List | `ports`, `volumes`, `expose` | Entries are appended |
Some lists are not simply appended. For volumes, entries that share a container path are merged into one, and the later entry wins. Run docker compose config whenever you are unsure what a merge produced.
Combining files with include
Merging layers files onto the same services. The top-level include element does something different. It pulls in another Compose file as a self-contained unit, which suits a stack owned by another team or a shared component such as a database.
include:
- ./infra/compose.yaml
services:
web:
build: .
depends_on:
- dbHere db is defined in ./infra/compose.yaml. Compose resolves relative paths in an included file against that file's own directory. An included file is not merged into your services. If it defines a service with the same name as one of yours, Compose reports a conflict instead of overriding it.
Choosing between them
- Use an override file for local-only changes to services you already define.
- Use
-fto select an environment, such as production, at the command line. - Use
includeto reuse a complete, independent Compose application.
Exercise
Your compose.yaml publishes web on "8000:8000". You run docker compose -f compose.yaml -f compose.prod.yaml up, and compose.prod.yaml sets ports to ["80:8000"] for web. Which ports does web publish, and how would you make it publish only port 80?
Show answer
It publishes both 8000 and 80, because lists such as ports are appended during a merge. To publish only 80, mark the key in compose.prod.yaml with the !override tag (ports: !override ["80:8000"]). You can check the result with docker compose -f compose.yaml -f compose.prod.yaml config.