Profiles & Override Files
One compose file rarely fits every situation. Developers want hot reload and an admin UI, CI wants a test runner, production wants none of that. Compose offers two tools to adapt one project to many uses: profiles switch optional services on and off, and override files change settings per environment.
Profiles
A service with a profiles: list only starts when one of its profiles is active. Services without profiles always start.
services:
api:
build: .
db:
image: postgres:17
adminer:
image: adminer:5
ports: ["8081:8080"]
profiles: [debug]
mailpit:
image: axllent/mailpit:v1.27
profiles: [debug, mail]
tests:
build: .
command: ["pytest", "-q"]
profiles: [test]docker compose up -d # api, db
docker compose --profile debug up -d # api, db, adminer, mailpit
COMPOSE_PROFILES=test docker compose up # api, db, tests
docker compose run --rm tests # naming a service directly activates itimport yaml
spec = yaml.safe_load("""
services:
api: {}
db: {}
adminer: {profiles: [debug]}
mailpit: {profiles: [debug, mail]}
tests: {profiles: [test]}
""")
def selected(active, explicit=()):
chosen = []
for name, service in spec["services"].items():
profiles = set(service.get("profiles", []))
if not profiles or profiles & set(active) or name in explicit:
chosen.append(name)
return chosen
print("no profile: ", selected([]))
print("--profile debug: ", selected(["debug"]))
print("--profile mail: ", selected(["mail"]))
print("profiles test,debug:", selected(["test", "debug"]))
print("run tests: ", selected([], explicit=["tests"]))no profile: ['api', 'db']
--profile debug: ['api', 'db', 'adminer', 'mailpit']
--profile mail: ['api', 'db', 'mailpit']
profiles test,debug: ['api', 'db', 'adminer', 'mailpit', 'tests']
run tests: ['api', 'db', 'tests']
Override files
By default docker compose reads compose.yaml and, if present, compose.override.yaml, merging the second on top of the first. With -f you choose the files explicitly; later files override earlier ones:
docker compose up -d # compose.yaml + compose.override.yaml
docker compose -f compose.yaml -f compose.prod.yaml up -d # base + production tweaks
docker compose -f compose.yaml -f compose.prod.yaml config # see the merged resultA typical split:
# compose.yaml: what every environment shares
services:
api:
image: ghcr.io/acme/shop-api:2.4.1
environment:
LOG_LEVEL: info
ports: ["8000:8000"]# compose.override.yaml: local development, picked up automatically
services:
api:
build: .
environment:
LOG_LEVEL: debug
RELOAD: "1"
volumes: ["./src:/app/src"]
ports: ["5678:5678"] # debuggerHow merging works
| Kind of field | Merge rule |
|---|---|
single values (image, command, restart) |
the later file replaces the value |
maps (environment, labels) |
merged by key; later keys win |
ports, expose, dns |
lists are concatenated |
volumes, devices |
merged by target path inside the container |
import yaml
base = yaml.safe_load("""
api:
image: ghcr.io/acme/shop-api:2.4.1
command: ["gunicorn", "shop.wsgi"]
environment: {LOG_LEVEL: info, WORKERS: "4"}
ports: ["8000:8000"]
volumes: ["static:/app/static"]
""")
override = yaml.safe_load("""
api:
build: .
command: ["python", "manage.py", "runserver", "0.0.0.0:8000"]
environment: {LOG_LEVEL: debug, RELOAD: "1"}
ports: ["5678:5678"]
volumes: ["./src:/app/src", "./static-dev:/app/static"]
""")
def merge_service(a, b):
out = dict(a)
for key, value in b.items():
if key in ("environment", "labels"):
out[key] = {**a.get(key, {}), **value}
elif key in ("ports", "expose", "dns"):
out[key] = a.get(key, []) + value
elif key == "volumes":
by_target = {v.split(":")[1]: v for v in a.get(key, [])}
by_target.update({v.split(":")[1]: v for v in value})
out[key] = list(by_target.values())
else:
out[key] = value
return out
merged = merge_service(base["api"], override["api"])
for key, value in merged.items():
print(f"{key:<12} {value}")image ghcr.io/acme/shop-api:2.4.1
command ['python', 'manage.py', 'runserver', '0.0.0.0:8000']
environment {'LOG_LEVEL': 'debug', 'WORKERS': '4', 'RELOAD': '1'}
ports ['8000:8000', '5678:5678']
volumes ['./static-dev:/app/static', './src:/app/src']
build .
The static named volume was replaced by the bind mount, because both target /app/static. The environment kept WORKERS from the base file. Ports were added, not replaced.
Tip: to remove something inherited from the base file, Compose supports the YAML tags
!reset(clear a value:ports: !reset []) and!override(replace a list or map instead of merging it).
include and extends
For larger setups, a compose file can include other compose files as sub-projects, and a service can extend a service from another file:
include:
- ./monitoring/compose.yaml # brings in prometheus + grafana
services:
worker:
extends:
file: ./common.yaml
service: python-base
command: ["celery", "-A", "shop", "worker"]A sensible layout
| File | Contents | Committed? |
|---|---|---|
compose.yaml |
services, images, networks, volumes shared by all environments | yes |
compose.override.yaml |
dev-only: build, bind mounts, debug ports, profiles: [debug] tools |
yes |
compose.prod.yaml |
restart policies, resource limits, no source mounts | yes |
.env |
local values for interpolation | no (commit .env.example) |
Go deeper: Profiles, Override Files & Merging and include in the Docker Compose course.
Test yourself
A service has profiles: [debug]. When does docker compose up start it?
- Always
- Only when the debug profile is active (or the service is named explicitly)
- Never
- Only in production
The base file has ports: ["8000:8000"], the override has ports: ["5678:5678"]. What is the merged result?
- Only 5678:5678
- Only 8000:8000
- Both ports
- An error
Which file does Compose merge automatically on top of compose.yaml when you don't pass -f?
- compose.prod.yaml
- compose.override.yaml
- docker-compose.dev.yml
- .env