Sub topic 7
Putting optional services in profiles
This chapter assumes you can read a Compose file, run docker compose up and down for a multi-service app, and set shell environment variables. It builds on the base file from earlier in the tutorial, which defines web and db.
The problem profiles solve
Some services are not part of the application itself. A database admin page helps while you debug. A migration job should run once, on demand. A load generator belongs in test runs and nowhere else. If you put these in the base file with no further controls, docker compose up starts all of them every time.
You could move them into an override file. That works, but then the only way to turn them on is to include the whole file, and that file often changes other settings too. A profile is a label on a service that makes it optional. The service is skipped unless someone activates a profile it carries.
The working model
Compose decides which services belong to the project before it creates anything. The rule has three parts:
- A service with no
profilesattribute is always enabled. - A service with a
profileslist is enabled only if at least one of its profiles is active. - A service that is not enabled is treated as if it were absent from the file. Commands such as
up,ps,logs, andconfigdo not see it.
The last point causes most of the surprises later in this chapter.
Assigning services to profiles
Add a profiles list to each optional service. This is the block added to the base file:
# Added to compose.yaml
services:
adminer:
image: adminer
profiles: [debug]
ports:
- "8080:8080"
migrate:
image: example/web:1.0
command: ["python", "manage.py", "migrate"]
profiles: [tools]
depends_on:
- dbWith the full base file, web and db have no profile, so they always start. adminer belongs to debug. migrate belongs to tools.
Run the plain command and only the unprofiled services appear:
DB_PASSWORD=secret docker compose up -d
docker compose ps --servicesThe second command prints web and db. The DB_PASSWORD value comes from the ${DB_PASSWORD:?set DB_PASSWORD} expression in the base file. Compose stops with that error message if the variable is missing, even if you only wanted to look at the configuration.
Profile names and multiple profiles
A profile name must start with a letter or digit and may then contain letters, digits, _, ., and -. Pick short, lowercase names that describe a purpose, such as debug, tools, or test.
A service can list more than one profile. It starts when any one of them is active, not only when all are:
services:
seed-data:
image: example/web:1.0
command: ["python", "manage.py", "loaddata", "sample"]
profiles: [debug, test]
depends_on:
- dbseed-data starts with --profile debug, with --profile test, or with both.
Activating profiles
You have three ways to switch profiles on. The next chapter covers them in detail, so here is the short form:
# Flag: the default services plus everything in the debug profile
DB_PASSWORD=secret docker compose --profile debug up -d
# Environment variable: several profiles, comma separated
DB_PASSWORD=secret COMPOSE_PROFILES=debug,tools docker compose up -d
# Name a profiled service directly: its profiles are enabled for that command
DB_PASSWORD=secret docker compose run --rm migrateYou can repeat the flag to activate more than one profile: --profile debug --profile tools. The --profile flag goes before the subcommand (up, run, and so on).
Activating a profile adds services. It never removes the unprofiled ones. docker compose --profile debug up starts web, db, and adminer.
Checking what a profile changes
Use config --services to see exactly which services a profile selects, without starting anything:
DB_PASSWORD=secret docker compose config --services
DB_PASSWORD=secret docker compose --profile debug config --servicesThe first command lists web and db. The second also lists adminer. Make this a habit when you add or rename a profile, because a typo in a profile name produces no error. The service is simply never selected.
Dependencies between profiled and unprofiled services
Dependencies are the most common source of profile errors. The direction matters.
| Dependency | Result |
|---|---|
| Profiled service depends on an unprofiled service | Works. `migrate` depends on `db`, and `db` is always enabled. |
| Profiled service depends on a service in the same profile | Works. Both are enabled together. |
| Unprofiled service depends on a profiled service | Fails when that profile is inactive, because the dependency does not exist in the project. |
| Service depends on a service in a different profile | Fails unless that other profile is also active. |
The failing cases show up as an error such as service "web" depends on undefined service "adminer". The fix is to reverse the relationship or to put both services in the same profile. A base service should never need an optional one.
# Wrong: web is always enabled, but adminer is optional
services:
web:
image: example/web:1.0
depends_on:
- adminer
adminer:
image: adminer
profiles: [debug]If the optional service needs the main one, express it that way. adminer can depend on db. web should not depend on adminer.
Profiled services and the lifecycle commands
Because inactive services are invisible, lifecycle commands only touch what is currently enabled. This catches people out in two ways.
Suppose you started the stack with --profile debug yesterday. Today you run:
docker compose downCompose sees only web and db, so it removes those. The adminer container stays behind, and the project network may stay too because a container still uses it. Pass the same profile when you tear down:
docker compose --profile debug downThe same applies to logs, ps, stop, and restart. A profiled container that is running does not appear in docker compose ps unless you pass its profile.
Compose also supports the wildcard --profile "*" in recent versions. It enables every profile in the project, which is useful for a full teardown. Check docker compose version before you rely on it in scripts.
Profiles and `run`
docker compose run <service> is the one place where you do not need to pass a flag. Naming a profiled service on the command line enables its profiles for that command, so docker compose run --rm migrate works with no --profile. The dependencies of the service you name must still be unprofiled or in the same profile. --rm removes the one-off container when it exits.
Targeting works the same way with up. docker compose up -d adminer starts adminer and, because it is named, enables its profile. It starts only the named service and its dependencies, so web is not started.
Profiles are not an isolation boundary
A profile only controls whether Compose includes a service. It does not hide the service definition. Anyone who can read the file sees adminer, and any user who can run Compose can activate the profile. Do not use profiles to keep a service out of production for security reasons. Use a separate file that production never loads. The next chapters compare these approaches.
Also note that profiles do not change merge behavior. When several files define the same service, Compose merges them first and then applies the profile rules to the merged result. If one file gives adminer profiles: [debug] and another file defines adminer with no profile, the merge result decides what happens, so check it with docker compose config.
Common errors
- Typo in a profile name.
--profile degubselects nothing and raises no error. Confirm withconfig --services. - Unprofiled service depending on a profiled one. The project fails to load. Reverse the dependency or share the profile.
- Leftover containers after
down. Pass the same--profileflags you used forup. - Assuming a profile removes services. Profiles only add. Unprofiled services always run.
- Expecting
COMPOSE_PROFILESto override the flag. They add together. Both sets of profiles become active.
Exercise
A file defines web (no profile), db (no profile), and metrics (profiles: [monitoring]). You run docker compose --profile monitoring up -d on Monday. On Tuesday you run docker compose down. Which containers are removed, and what would you change to remove all three?
Show answer
down removes only web and db, because metrics is in an inactive profile and Compose ignores it. The metrics container keeps running. To remove all three, run docker compose --profile monitoring down, or set COMPOSE_PROFILES=monitoring for that command.
Exercise
You add depends_on: [adminer] to web, and adminer has profiles: [debug]. docker compose up now fails with an error about an undefined service. Why, and how do you fix it?
Show answer
web is always enabled, but adminer is not enabled without the debug profile, so Compose treats it as missing and the dependency cannot be resolved. Remove the dependency from web. If the two really must start together, give web the same profile only when that makes sense, or make adminer depend on db or web instead so the optional service depends on the core one.