Sub topic 10
Choosing between overrides and profiles, and using both
This chapter assumes you can read Compose YAML, know how compose.override.yaml and -f select files, and know how --profile and COMPOSE_PROFILES activate services. Here you decide which tool fits which need, then combine them in one project.
What each tool changes
Override files change how a service is defined. Profiles change whether a service runs at all. Keeping that distinction in mind answers most design questions.
| Need | Better tool | Why |
|---|---|---|
| Different `LOG_LEVEL` per environment | Override file | The same service gets different values. |
| Mount source code only in development | Override file | You are editing an existing service. |
| Add `restart: unless-stopped` in production | Override file | Same service, extra attributes. |
| Run a database admin UI only when debugging | Profile | The service is optional, not different. |
| Run migrations on demand | Profile | The service is a one-off task. |
| Run integration tests in CI only | Profile or override | A profile suits a test runner. An override suits a change to `web`. |
A short rule: if the question is "how should this service look here?", use an override. If it is "should this service exist here?", use a profile.
Why not use only one
Using only profiles for settings gets awkward. A profile cannot change LOG_LEVEL on web, because web has no profile and always starts. You would need web-dev and web-prod copies, which duplicate configuration.
Using only overrides for optional services is also clumsy. You would have to add adminer to a file and remember to include that file every time you want it. A profile keeps adminer in the base file and turns it on with one flag.
Using both together
A common layout keeps optional services in the base file under profiles and keeps environment-specific settings in override files.
# compose.yaml (base)
services:
web:
image: example/web:1.0
environment:
LOG_LEVEL: info
depends_on:
- db
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD}
adminer:
image: adminer
profiles: [debug]
ports:
- "8080:8080"
tests:
image: example/web:1.0
command: ["python", "-m", "pytest"]
profiles: [test]
depends_on:
- db# compose.prod.yaml
services:
web:
restart: unless-stopped
environment:
LOG_LEVEL: warning
db:
restart: unless-stoppedThe files and profiles combine independently:
# Development: automatic override file, plus the admin UI
DB_PASSWORD=dev docker compose --profile debug up -d
# CI: base file only, plus the test runner
DB_PASSWORD=ci docker compose --profile test run --rm tests
# Production: explicit files, no profiles
DB_PASSWORD="$PROD_DB_PASSWORD" docker compose -f compose.yaml -f compose.prod.yaml up -dThe file list decides the definitions. The profile list decides which of those definitions start.
Pitfalls when combining
- A profile attribute in an override file works, but it makes the service's behavior harder to find. Prefer declaring
profileswhere the service is first defined. - Profile activation does not depend on which files you load. If an override file adds
profiles: [debug]toweb,webstops starting without that profile. - A service with no profile that depends on a profiled service will fail to start unless that profile is active. Give dependents the same profile, or avoid the dependency.
docker compose configshows only active services. Run it with the profiles you intend to use, for exampledocker compose --profile debug config --services.
Exercise
Your team wants LOG_LEVEL=debug and a bind-mounted source directory on developer machines, and a pgadmin container that developers start only when needed. Which parts belong in an override file and which in a profile?
Show answer
The LOG_LEVEL value and the bind mount change the existing web service, so they belong in compose.override.yaml. pgadmin is an optional service, so define it in the base file with profiles: [debug] (or a similar name) and start it with --profile debug.
Exercise
You run docker compose -f compose.yaml -f compose.prod.yaml --profile debug up -d. Does adminer start, and does compose.override.yaml apply?
Show answer
adminer starts because the debug profile is active and the service is defined in compose.yaml. compose.override.yaml does not apply, because -f disables the automatic override file.