Sub topic 10
Choosing between overrides and profiles, and using both
This chapter assumes you can read Compose YAML. It also assumes you know how compose.override.yaml and -f select files, and that you have seen profiles on a service. Here you will learn when each tool is the right one, and how to combine them without surprises.
Two different questions
Override files and profiles solve different problems.
- An override file answers: how should this service be defined in this environment? It changes values of services that already exist, such as the image, build context, volumes, environment variables, or restart policy.
- A profile answers: which services should run in this invocation? It does not change how a service is defined. It only decides whether the service is part of the run.
Both can add a service to the project, which is why they get confused. The difference is what happens afterward. A service added by an override file is present whenever that file is loaded. A service with a profile is present only when the profile is active.
| Need | Better tool | Why |
|---|---|---|
| Build locally in development, pull a tagged image in production | Override file | The same service has different definitions |
| Mount source code only in development | Override file | It changes a service's volumes |
| Different `restart` policy in production | Override file | It changes a service's attributes |
| Database admin UI you sometimes want | Profile | The service is optional, and its definition is the same everywhere |
| One-off migration or seed job | Profile | The service should run only when asked |
| End-to-end test runner that must not start in production | Profile, often inside a test file | The service is not part of the normal stack |
| A service that exists only in one environment, always on there | Override file | No switch is needed. Loading the file is the switch |
A decision flow
A useful rule: if you would write "in production, the web service should..." you want an override file. If you would write "sometimes I also want..." you want a profile.
A layout that uses both
The base file holds the shared services and the optional tools. The tools live in the base file because their definition is the same everywhere. Only their activation differs.
# compose.yaml (base)
services:
web:
image: example/web:1.0
ports:
- "8000:8000"
environment:
LOG_LEVEL: info
depends_on:
- db
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD}
volumes:
- db-data:/var/lib/postgresql/data
adminer:
image: adminer
profiles: [debug]
ports:
- "8080:8080"
migrate:
image: example/web:1.0
command: ["python", "manage.py", "migrate"]
profiles: [tools]
depends_on:
- db
volumes:
db-data:The development override changes how web is defined. It also adds a mail catcher that should always run in development, so it needs no profile.
# compose.override.yaml (loaded automatically)
services:
web:
build: .
volumes:
- ./src:/app/src
environment:
LOG_LEVEL: debug
mailpit:
image: axllent/mailpit
ports:
- "8025:8025"The production file only changes definitions:
# compose.prod.yaml
services:
web:
restart: unless-stopped
environment:
LOG_LEVEL: warning
db:
restart: unless-stoppedThe test file adds a runner that exists only for test runs, and the runner is also behind a profile:
# compose.test.yaml
services:
e2e:
image: example/e2e:1.0
profiles: [e2e]
environment:
BASE_URL: http://web:8000
depends_on:
- webHere is what each command starts. All of them need DB_PASSWORD set, because the base file requires it.
export DB_PASSWORD=devpass
# web, db, mailpit (override file loaded automatically; no profiles active)
docker compose up -d
# web, db, mailpit, adminer
docker compose --profile debug up -d
# web, db only: -f drops the automatic override, so no mailpit and no local build
docker compose -f compose.yaml -f compose.prod.yaml up -d
# Run migrations on demand. Naming the service activates its profile
docker compose run --rm migrate
# Run the end-to-end suite against the stack
docker compose -f compose.yaml -f compose.test.yaml up -d web
docker compose -f compose.yaml -f compose.test.yaml run --rm e2eIn the last pair, up -d web starts only web and its dependency db. The run command then starts e2e because you named it, which activates the e2e profile for that command.
Where the two mechanisms interact
Profiles are evaluated after files are merged
Compose first merges all loaded files into one model. It then removes services whose profiles are not active. So a service defined in the base file with profiles: [debug] is dropped in every command that does not activate debug, whichever override files you load. You can check this with config --services:
docker compose -f compose.yaml -f compose.prod.yaml config --services
docker compose -f compose.yaml -f compose.prod.yaml --profile debug config --servicesThe first prints web and db. The second also prints adminer.
Overrides can configure a profiled service
You can adjust a profiled service in an override file even when the profile is off. The change is merged into the model and takes effect only when the profile is active. For example, to bind Adminer to localhost in development only:
# compose.override.yaml (addition)
services:
adminer:
ports: !override
- "127.0.0.1:8080:8080"Without !override, the list would be appended to the base file's "8080:8080". That would publish two mappings, including the open one. The !override tag needs a Compose version that supports it.
Dependencies must be available
A service that depends on another service needs that dependency to be part of the run. If a service with no profile lists a profiled service in depends_on, Compose reports an error unless that profile is active. Keep the dependency pointing the other way. The profiled migrate service depends on db, which has no profile. The always-on services never depend on optional ones.
Pitfalls specific to combining them
- A stray
COMPOSE_PROFILES. The variable is read from your shell and from a.envfile. If a developer's.envsetsCOMPOSE_PROFILES=debug, that value is also active wherever the same.envis present. On a production host, remove it or override it. Runconfig --servicesbefore deploying. - Forgetting that
-fdrops the automatic override file. Production commands that list only-f compose.yaml -f compose.prod.yamlare correct. Commands that rely on the default file but add-f compose.prod.yamlalone will losecompose.yaml. - Using a profile where an override is needed. Two copies of the same service,
web-devandweb-prod, each in its own profile, duplicate everything and drift apart. If it is the same service with different settings, use an override file. - Using an override where a profile is needed. A debug tool added in
compose.override.yamlstarts every time in development. That is fine for a mail catcher, but wasteful for a heavy tool you use once a week. - Profile names as environment names. Profiles such as
devandprodsuggest that one file switches everything. They only filter services. They cannot change the image ofweb.
Choosing under real constraints
Some decisions are not clear-cut. These criteria help:
- Count the environments that differ. If three environments need three different
webdefinitions, that is three override files. Profiles cannot express this. - Count the people who toggle it. If developers choose per session, as with Adminer, a profile gives them a flag. If the toggle is a deployment decision, use a file, because it is reviewed in version control.
- Check what fails safely. A forgotten profile means a tool does not start, which is mild. A forgotten production file means development settings run in production, which is serious. Make production selection explicit with
-forCOMPOSE_FILEin your deploy script. - Prefer fewer files. One base, one development override, one production file, and optionally one test file is usually enough. More files make merged results harder to reason about.
Exercise
Your team wants (1) a Mailpit container that always runs for developers, (2) a Redis Commander UI that developers start only when debugging, and (3) restart: unless-stopped on web in production only. Say where each belongs (base file, override file, production file, or profile), and give the command that starts the production stack.
Show answer
(1) Put mailpit in compose.override.yaml with no profile. It loads automatically in development and is absent from production because -f disables the override file. (2) Put redis-commander in the base file with profiles: [debug], and start it with docker compose --profile debug up -d. Its definition is the same everywhere, so it only needs a switch. (3) Put restart: unless-stopped under web in compose.prod.yaml, because it changes an existing service's definition. The production command is docker compose -f compose.yaml -f compose.prod.yaml up -d. Before running it, check that COMPOSE_PROFILES is not set, for example with docker compose -f compose.yaml -f compose.prod.yaml config --services.
Exercise
A .env file in the project contains COMPOSE_PROFILES=debug. A colleague runs docker compose -f compose.yaml -f compose.prod.yaml up -d on a server that has the same .env. What happens, and how do you prevent it?
Show answer
Compose reads COMPOSE_PROFILES from .env, so the debug profile is active even though the command line does not mention it. Adminer starts and publishes port 8080 in production. To prevent it, keep COMPOSE_PROFILES out of committed .env files, or unset it on the server. Also run config --services with the production flags in the deploy script to confirm that only the expected services are listed.