Sub topic 8
Turning profiles on: --profile, COMPOSE_PROFILES, and targeted services
This chapter assumes you can read Compose YAML, run docker compose up for a multi-service app, and set shell environment variables. It also assumes you have read the previous chapter, where optional services were given a profiles attribute. Here you learn the three ways to activate those profiles, and the rules that decide which services start.
The starting rule
A service with no profiles attribute is always enabled. A service with a profiles attribute is disabled until one of its profiles is active. Profiles only decide which services are enabled. They don't change a service's configuration.
Here is the file used throughout this chapter, with two profiled services added to the base services:
# compose.yaml
services:
web:
image: example/web:1.0
ports:
- "8000:8000"
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:With no profile active, docker compose up -d starts web and db only. adminer and migrate are ignored.
Three ways to activate a profile
The --profile flag
Pass --profile before the subcommand. Repeat it to activate several profiles.
# Default services plus adminer
docker compose --profile debug up -d
# Default services plus adminer and migrate
docker compose --profile debug --profile tools up -dThe flag belongs to docker compose, not to up. Writing docker compose up --profile debug fails with an unknown flag error.
The COMPOSE_PROFILES variable
Set COMPOSE_PROFILES to a comma-separated list. This is useful in CI jobs, in a shell profile, or in a .env file.
COMPOSE_PROFILES=debug,tools docker compose up -dIf you put it in the project's .env file, every Compose command in that directory activates those profiles:
# .env
COMPOSE_PROFILES=debugWhen you pass --profile on the command line, it takes precedence over COMPOSE_PROFILES. The two are not combined. To be sure what runs, check with docker compose config --services.
Naming a service directly
When you name a profiled service in a command, Compose activates that service's profiles for you. This works for commands that take service names, such as run, up, and start.
# No --profile needed; naming migrate activates its profile
docker compose run --rm migrateThis is the usual way to run one-off tasks such as migrations, seed scripts, and backups. The task lives in the same file as the application, yet it never starts during a normal up.
Targeting has a limit. Compose activates the profiles of the service you name, and it also starts that service's dependencies. The migrate service depends on db, which has no profile, so db starts. Targeting works the same way when the dependency is profiled, as the next section explains.
Dependencies and profiles
A profiled service can depend on a service in a different profile. What happens depends on how you start it.
- If you target the service by name, Compose activates the profiles of the targeted service. A dependency in another profile is the case to be careful with.
- If you start with
--profileorCOMPOSE_PROFILES, only the services in the active profiles (plus services without a profile) are enabled. A dependency in an inactive profile makes the project invalid.
Consider this case:
services:
api:
image: example/api:1.0
profiles: [app]
depends_on:
- cache
cache:
image: redis:7
profiles: [infra]Running docker compose --profile app up fails, because api needs cache and cache is disabled. Compose reports that a service depends on an undefined or disabled service. You have three fixes:
- Activate both profiles:
docker compose --profile app --profile infra up. - Give
cachemore than one profile:profiles: [infra, app]. - Remove the profile from
cacheso it always runs.
Option 2 is the cleanest when a service is shared. A service with several profiles starts when any one of them is active.
services:
cache:
image: redis:7
profiles: [infra, app]When a profiled service needs a dependency that should always exist, leave the dependency without a profile. That removes the whole class of error.
What changes for other commands
Profiles affect which services a command sees. Commands that act on the whole project, such as down, ps, logs, and stop, also only see enabled services.
This causes a common surprise. You start a project with a profile, then run docker compose down without it:
docker compose --profile debug up -d
docker compose downThe adminer container keeps running, because down didn't consider it part of the project. Use the same profile selection for down as for up:
docker compose --profile debug downAnother option is to activate every profile at once with the wildcard *:
docker compose --profile "*" downQuote the asterisk so the shell doesn't expand it. The same value works in the variable form: COMPOSE_PROFILES='*'.
Because the wildcard enables every profiled service, use it for cleanup, not for starting your application.
Verifying what will start
Before starting anything, ask Compose which services are enabled for a given selection:
# Default services only
docker compose config --services
# With the debug profile
docker compose --profile debug config --services
# With a variable instead of a flag
COMPOSE_PROFILES=tools docker compose config --servicesThe output is one service name per line. This is a fast check when a service does not start, or when an unexpected service does.
Choosing an activation method
| Method | Best for | Scope |
|---|---|---|
| `--profile` | Interactive work, scripts that name profiles explicitly | One command |
| `COMPOSE_PROFILES` | CI jobs, shell sessions, `.env` defaults | Every command that sees the variable |
| Targeting a service | One-off tasks such as `run --rm migrate` | The named service and its dependencies |
Common mistakes
- Putting
--profileafter the subcommand. It is a global option and goes right afterdocker compose. - Forgetting the profile on
down. Profiled containers stay running and may hold ports. - Depending on a service in an inactive profile. Give shared services no profile, or list every profile that needs them.
- Mixing the flag and the variable and expecting both to apply. The flag wins. Print
config --servicesto confirm. - Using profiles for settings that differ per environment. Profiles switch services on and off. Use override files for changes to a service's settings, as covered in earlier chapters.
Putting it together
A typical day with the file above looks like this:
export DB_PASSWORD=devpass
# Start the app
docker compose up -d
# Run a migration without starting anything else new
docker compose run --rm migrate
# Open the database browser while debugging
docker compose --profile debug up -d adminer
# Clean up everything, including profiled containers
docker compose --profile "*" downDB_PASSWORD is exported first because the base file refuses to start without it. The run command activates tools through targeting. The up -d adminer line shows that a flag and a named service can be used together. The final command uses the wildcard so nothing is left behind.
Exercise
You run docker compose --profile debug up -d to start your app and the adminer service. Later you run docker compose down, but docker compose ps still lists adminer as running. Why does this happen, and what are two ways to remove it?
Show answer
down only acts on enabled services, and adminer is in the debug profile, which was not active for the down command. Fix it by repeating the profile (docker compose --profile debug down) or by activating all profiles (docker compose --profile "*" down, or COMPOSE_PROFILES='*' docker compose down). You could also stop it by name with docker compose rm -sf adminer, since naming a service activates its profile.
Exercise
A service api has profiles: [app] and depends_on: [cache], and cache has profiles: [infra]. Running docker compose --profile app up fails. Give two changes to the file that fix it without changing the command.
Show answer
Add app to the cache service's profiles (profiles: [infra, app]), or remove the profiles attribute from cache so it always runs.