Override Files & Merging
One application, several environments. On your laptop you want source code mounted, a debugger port and verbose logs. In CI you want a test runner and no published ports. In production you want pinned images, restart policies and resource limits. Copy-pasting three nearly identical compose files means three places to fix every change.
Compose's answer is layering: a base file with what every environment shares, plus small override files that change only what differs. Compose merges them, in order, into one configuration. This lesson shows how the merge works, field by field, and the two tags (!reset, !override) that change it.
compose.override.yaml is loaded automatically
When you run docker compose without -f, Compose reads compose.yaml and, if it exists, compose.override.yaml from the same folder. That makes the override file the natural home for development settings:
services:
api:
image: ghcr.io/acme/api:3.0
command: ["gunicorn", "app:app", "--bind", "0.0.0.0:8000"]
ports: ["8000:8000"]
environment:
LOG_LEVEL: info
WORKERS: "4"
labels: [tier=api]
# --- compose.override.yaml
services:
api:
command: ["flask", "run", "--debug", "--host", "0.0.0.0", "--port", "8000"]
ports: ["5678:5678"]
environment:
LOG_LEVEL: debug
labels: [dev=true]name: demo
services:
api:
command:
- flask
- run
- --debug
- --host
- 0.0.0.0
- --port
- "8000"
environment:
LOG_LEVEL: debug
WORKERS: "4"
image: ghcr.io/acme/api:3.0
labels:
dev: "true"
tier: api
networks:
default: null
ports:
- mode: ingress
target: 8000
published: "8000"
protocol: tcp
- mode: ingress
target: 5678
published: "5678"
protocol: tcp
networks:
default:
name: demo_default
Look at how each key was combined:
command(a single value) was replaced by the override's.portswere appended: both 8000 and 5678 are published.environmentwas merged by key:LOG_LEVELwas overridden,WORKERSkept.labelswere merged by key too, after normalising the list[tier=api]into a mapping.
The merge rules
| Kind of field | Examples | Rule |
|---|---|---|
| single values | image, command, entrypoint, user, restart, healthcheck.test |
later file wins |
| mappings | environment, labels, deploy.resources, build.args, extra_hosts |
merged key by key, later wins per key |
| sequences that are sets | ports, expose, dns, dns_search, tmpfs |
concatenated, duplicates dropped |
| mounts | volumes, devices |
merged by container path (target): same target is replaced |
| references to other items | secrets, configs, networks |
merged by name |
The mount rule is the one that surprises people. Here the base mounts a named volume at /data and the override wants a bind mount there instead. Same target, so the override replaces it rather than adding a second mount:
# $ docker compose config --no-path-resolution
services:
api:
image: ghcr.io/acme/api:3.0
volumes:
- data:/data
- logs:/var/log/app
volumes:
data:
logs:
# --- compose.override.yaml
services:
api:
volumes:
- ./fixtures:/dataname: demo
services:
api:
image: ghcr.io/acme/api:3.0
networks:
default: null
volumes:
- type: bind
source: ./fixtures
target: /data
bind: {}
- type: volume
source: logs
target: /var/log/app
volume: {}
networks:
default:
name: demo_default
volumes:
logs:
name: demo_logs
The data volume vanished from the top level as well: no service uses it any more.
Choosing files with -f
With -f you name the files yourself, and compose.override.yaml is no longer loaded automatically. Files are merged left to right, and the first file's folder becomes the project directory (relative paths resolve from there).
# $ docker compose config --services
# $ docker compose -f compose.yaml -f compose.prod.yaml config --services
services:
api:
image: ghcr.io/acme/api:3.0
# --- compose.override.yaml
services:
debugger:
image: ghcr.io/acme/debug-tools:1.0
# --- compose.prod.yaml
services:
backup:
image: ghcr.io/acme/backup:2.4$ docker compose config --services
api
debugger
$ docker compose -f compose.yaml -f compose.prod.yaml config --services
api
backup
Typing -f every time gets old. The variable COMPOSE_FILE lists the files (separated by : on Linux/macOS, ; on Windows) and can live in .env, so each machine picks its stack once:
# $ docker compose config --services
services:
api:
image: ghcr.io/acme/api:3.0
# --- compose.override.yaml
services:
debugger:
image: ghcr.io/acme/debug-tools:1.0
# --- compose.ci.yaml
services:
tests:
image: ghcr.io/acme/api-tests:3.0
# --- .env
COMPOSE_FILE=compose.yaml:compose.ci.yamlapi
tests
Removing things: !reset and !override
Merging can only add or change, which is a problem when production must remove something the base defines, such as a debug port or a variable. Two YAML tags fix that:
!resetremoves the value inherited from earlier files (!reset null,!reset []or!reset {}).!overridereplaces the value entirely instead of merging it.
# $ docker compose -f compose.yaml -f compose.prod.yaml config
services:
api:
image: ghcr.io/acme/api:3.0
ports: ["8000:8000", "5678:5678"]
environment:
DEBUG: "1"
LOG_LEVEL: info
# --- compose.prod.yaml
services:
api:
ports: !override ["8000:8000"]
environment:
DEBUG: !reset nullname: demo
services:
api:
environment:
LOG_LEVEL: info
image: ghcr.io/acme/api:3.0
networks:
default: null
ports:
- mode: ingress
target: 8000
published: "8000"
protocol: tcp
networks:
default:
name: demo_default
Without !override, ports would have stayed as both 8000 and 5678 (sequences are concatenated), and without !reset, DEBUG=1 would have leaked into production.
A model of the merge
A simplified merge in Python, following the rules in the table. It's useful to predict what a stack of files does before you run config:
REPLACE = {"image", "command", "user", "restart"}
BY_KEY = {"environment", "labels"}
APPEND_UNIQUE = {"ports", "dns"}
def merge_service(base, over):
out = dict(base)
for key, value in over.items():
if key in BY_KEY:
out[key] = {**base.get(key, {}), **value}
elif key in APPEND_UNIQUE:
out[key] = list(dict.fromkeys(base.get(key, []) + value))
elif key == "volumes": # merge by target (container path)
by_target = {v.split(":")[1]: v for v in base.get(key, [])}
by_target.update({v.split(":")[1]: v for v in value})
out[key] = list(by_target.values())
else:
out[key] = value # REPLACE and anything else
return out
base = {"image": "api:3.0", "ports": ["8000:8000"], "environment": {"LOG_LEVEL": "info", "WORKERS": "4"},
"volumes": ["data:/data", "logs:/var/log/app"]}
dev = {"ports": ["5678:5678", "8000:8000"], "environment": {"LOG_LEVEL": "debug"},
"volumes": ["./fixtures:/data"]}
prod = {"image": "api:3.0.7", "restart": "always"}
result = base
for layer in (dev, prod):
result = merge_service(result, layer)
for key, value in result.items():
print(f"{key:<12} {value}")image api:3.0.7
ports ['8000:8000', '5678:5678']
environment {'LOG_LEVEL': 'debug', 'WORKERS': '4'}
volumes ['./fixtures:/data', 'logs:/var/log/app']
restart always
Organising your files
A layout that scales well:
compose.yaml shared: services, networks, volumes, healthchecks
compose.override.yaml development (loaded by default): bind mounts, debug ports, watch
compose.ci.yaml CI: test runner, no ports, throw-away volumes
compose.prod.yaml production: pinned images, restart, resources, loggingAlways check the merged result with docker compose -f … -f … config before deploying. It's the single source of truth for what will run. Other ways to share configuration are extends (reuse one service in another), include (compose whole sub-projects) and profiles (optional services in one file).
Learn more: Compose in Production · Compose in CI · Kustomize overlays, the Kubernetes take on the same idea
Test yourself
The base file publishes 8000:8000, the override publishes 5678:5678. What does the merged service publish?
- Only 5678
- Only 8000
- Both 8000 and 5678
- An error: duplicate key
You run docker compose -f compose.yaml -f compose.prod.yaml up. Is compose.override.yaml loaded?
- Yes, always
- No, with -f only the named files are used
- Yes, but last
- Only if COMPOSE_PROFILES is set
How can a production override remove DEBUG set in the base environment?
- DEBUG: ""
- DEBUG: !reset null
- Delete it with --env-file
- It can't