Sub topic 11
Optional services with profiles
This chapter assumes you can read a compose.yaml and run docker compose up. It also assumes you know what a service is and how depends_on works.
The problem
A stack often contains services you don't need on every run. Examples are a database admin UI, a mail catcher, or a test runner. If they sit in the file with no special treatment, docker compose up starts all of them. A profile is a label on a service. Compose starts a labeled service only when you turn that label on.
Assigning a profile
Add a profiles list to a service:
services:
web:
image: myapp:latest
adminer:
image: adminer
profiles: ["debug"]
ports:
- "8080:8080"web has no profile, so it belongs to no group and always starts. adminer starts only when the debug profile is active.
docker compose up -d
docker compose --profile debug up -dThe first command starts only web. The second starts web and adminer.
Ways to activate a profile
- The
--profileflag. It is a flag ofdocker compose, so it goes before the subcommand. Repeat it to enable several profiles. - The
COMPOSE_PROFILESenvironment variable. Use a comma-separated list, such asCOMPOSE_PROFILES=debug,test. You can also put this line in your.envfile. - Naming the service. If you run
docker compose up adminerordocker compose run adminer, Compose enables that service's profiles for you. --profile "*"enables every profile defined in the file.
Common errors
A default service depends on a profiled one. Suppose web lists adminer under depends_on, but adminer has a profile. A plain docker compose up then fails, because the dependency is not part of the active set. Fix this in one of two ways. Give web the same profile, or remove the dependency. A profiled service may depend on an unprofiled one without trouble.
down leaves containers behind. Commands act only on services in the active profiles. If you started adminer with --profile debug, then docker compose down without the flag does not remove it. Pass the same profile to down, or set COMPOSE_PROFILES once for the whole session.
A typo in the profile name. Compose does not warn you about an unused profile. If --profile debgu matches nothing, the service silently stays off. Run docker compose --profile debug config --services to see which services a given profile selects.
Profiles compared with override files
Profiles switch whole services on and off inside one file. Override files change the settings of services that already exist. If a service should only exist sometimes, use a profile. If a service exists everywhere but its settings differ by environment, use an override file. A later chapter covers override files.
Exercise
Your file defines web with no profile and mailpit with profiles: ["dev"]. You run docker compose up -d, then later run docker compose --profile dev up -d. Which services run after each command, and what do you run to remove both?
Show answer
After the first command only web runs. After the second, web and mailpit run. To remove both, run docker compose --profile dev down. Without the flag, down would skip mailpit.
Exercise
A service named report has profiles: ["tools"]. How can you start it once without using the --profile flag or editing the file?
Show answer
Name it on the command line, for example docker compose run report. Compose enables the profiles of an explicitly named service automatically. Setting COMPOSE_PROFILES=tools is another option.