Compose Override
A Compose override file is a second Compose file merged on top of the base file to change settings for one environment. docker-compose.override.yml is merged automatically, and any other file is added with -f.
One base file, small environment files
Keep what every environment shares in compose.yaml, and put environment differences (bind mounts, ports, resource limits) in override files. Compose reads compose.override.yaml (or docker-compose.override.yml) automatically if it sits next to the base file. Files with other names must be named explicitly.
# compose.yaml (base)services: payment-api: image: payment-api:v3.1.0 environment: NODE_ENV: production# compose.override.yaml (dev, merged automatically)services: payment-api: build: . environment: NODE_ENV: development volumes: - ./src:/app/src ports: - "3000:3000"# compose.prod.yaml (production, use with -f)services: payment-api: restart: unless-stopped deploy: resources: limits: cpus: "1.5" memory: 512MChoosing files and previewing the merge
docker compose up -d # base + overridedocker compose -f compose.yaml -f compose.prod.yaml up -d # base + prod onlydocker compose -f compose.yaml -f compose.prod.yaml config # show merged resultHow merging behaves
- Single values such as
imageare replaced by the later file. - Mappings such as
environmentare merged key by key, and later values win. - Lists such as
portsandvolumesare combined, with volumes matched by mount path. commandandentrypointare replaced, not appended.
TipRun
docker compose configbefore deploying. It prints the final merged file, which is the fastest way to find unexpected values.
Common MistakePassing only
-f compose.prod.yaml. The automatic override is skipped when you use-f, so also pass the base file explicitly.
Frequently Asked Questions
Why use override files instead of separate full compose files?
Separate full files drift apart. With a base file and small overrides, shared definitions live in one place and each environment only states its differences, which is easier to review.
Why did I get duplicate ports after merging?
Lists like ports are appended across files, so the same mapping in both base and override can appear twice or conflict. Keep port mappings in only one file per environment.
Is docker-compose.override.yml loaded when I use -f?
No. The automatic loading only happens when you give no -f flags. Once you use -f, only the listed files are used, in order.