Skip to main content

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.

YAML
# compose.yaml (base)
services:
payment-api:
image: payment-api:v3.1.0
environment:
NODE_ENV: production
YAML
# compose.override.yaml (dev, merged automatically)
services:
payment-api:
build: .
environment:
NODE_ENV: development
volumes:
- ./src:/app/src
ports:
- "3000:3000"
YAML
# compose.prod.yaml (production, use with -f)
services:
payment-api:
restart: unless-stopped
deploy:
resources:
limits:
cpus: "1.5"
memory: 512M

Choosing files and previewing the merge

Bash
docker compose up -d # base + override
docker compose -f compose.yaml -f compose.prod.yaml up -d # base + prod only
docker compose -f compose.yaml -f compose.prod.yaml config # show merged result

How merging behaves

  • Single values such as image are replaced by the later file.
  • Mappings such as environment are merged key by key, and later values win.
  • Lists such as ports and volumes are combined, with volumes matched by mount path.
  • command and entrypoint are replaced, not appended.
Tip

Run docker compose config before deploying. It prints the final merged file, which is the fastest way to find unexpected values.

Common Mistake

Passing 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.

Found a mistake, an outdated step or a broken link?