Sub topic 8
Turning profiles on: --profile, COMPOSE_PROFILES, and targeted services
This chapter assumes you can read a Compose file, know how docker compose up and down work, and have already put optional services into profiles. Here you learn the three ways to activate a profile and the rules that decide which services start.
The starting point
The base file from the previous chapter has two services with no profiles attribute, web and db. It also has two profiled services:
# 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:
- dbA service without profiles always starts. A service with profiles starts only when at least one of its profiles is active. That rule explains every behavior below.
Three ways to activate a profile
The --profile flag
Pass --profile before the subcommand. Repeat it to activate several profiles.
# Start the default services plus the debug profile
docker compose --profile debug up -dThe COMPOSE_PROFILES variable
The same selection works through an environment variable. Separate several profiles with commas.
# Same thing with an environment variable; several profiles are comma separated
COMPOSE_PROFILES=debug,tools docker compose up -dThis form suits CI jobs and .env files, because you set it once and every command in that shell uses it. Setting COMPOSE_PROFILES=* activates every profile.
Naming a service directly
If you name a profiled service in a command such as run, Compose activates its profiles for you.
# Run a profiled service directly: naming it activates its profile automatically
docker compose run --rm migrateNo flag is needed here. Compose also starts db, because migrate depends on it and db has no profile.
Which services start
The diagram shows how Compose decides for each service.
Verifying the selection
Check the result before you start anything. config --services lists the services that would run with the given profile.
docker compose --profile debug config --servicesWith the files above, the output contains web, db, and adminer. It does not contain migrate, because tools is not active.
Behaviors that surprise people
upignores inactive profiled services. Runningdocker compose up migratewithout a flag works, because naming a service activates its profile. Running plaindocker compose upnever starts it.- Dependencies must be active too. If an always-on service depends on a profiled one, Compose reports an error, because the dependency would not start. Give the dependent service the same profile, or leave the dependency without one.
downneeds the profile.docker compose downstops only services in active profiles. To removeadminer, rundocker compose --profile debug down, or setCOMPOSE_PROFILES.- Profile names are strings you choose. A typo such as
--profile degubsilently activates nothing, soconfig --servicesis a cheap check.
Choosing a method
| Method | Best for | Scope |
|---|---|---|
| `--profile` | One-off commands at a terminal | A single command |
| `COMPOSE_PROFILES` | CI jobs, shell sessions, `.env` files | Every command in that environment |
| Naming a service | Tasks such as migrations | That command only |
Exercise
Your compose.yaml has web and db with no profile, plus adminer in debug and migrate in tools. What services start with COMPOSE_PROFILES=tools docker compose up -d, and what must you do to stop adminer if you started it earlier with --profile debug?
Show answer
web, db, and migrate start. adminer is not started because debug is inactive. To stop it, run docker compose --profile debug down (or set COMPOSE_PROFILES=debug), since down only affects services in active profiles.
Exercise
A teammate runs docker compose run --rm migrate without any --profile flag. Does it work, and why?
Show answer
Yes. Naming a profiled service in the command activates its profiles automatically. Its dependency db also starts because db has no profile.