Sub topic 7
Putting optional services in profiles
This chapter assumes you can read Compose YAML, know what a service, image, and volume are, and have run docker compose up and down for a multi-service app. It covers how to mark services as optional. Later chapters cover turning profiles on.
The problem profiles solve
Some services are useful only sometimes. A database admin UI helps while debugging but has no place in production. A migration job should run once, on demand, and not on every up.
An override file can add such services for one environment, but it ties the choice to the file set. A profile is a label on a service. A service with a profile starts only when that profile is active. A service with no profile always starts.
Assigning a service to a profile
You add a profiles attribute to the service. Its value is a list of profile names.
# Added to 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:Here web and db have no profile, so they are part of the default set. adminer belongs to debug. migrate belongs to tools.
Running the plain command starts only the default services:
docker compose up -dOnly web and db start. adminer and migrate are defined but stay off.
What the default set means
A service without profiles is always enabled, whichever profiles you activate. Activating debug adds services to the set. It never removes web or db.
A service can list several profiles, such as profiles: [debug, tools]. It starts if any one of them is active.
flowchart TDThe diagram below shows how Compose decides which services to start.
Dependencies and profiles
Profiles interact with depends_on. In the example, migrate depends on db. Because db has no profile, it is always available, and this works.
The reverse is a common error. Suppose an always-on service depends on a profiled one. If web listed adminer under depends_on, Compose would fail because the dependency is not enabled. Keep dependencies pointing from optional services toward default ones. If an optional service needs another optional service, give both a shared profile name.
Checking what a profile enables
You can check which services a profile selects without starting anything:
docker compose config --services
docker compose --profile debug config --servicesThe first command lists web and db. The second also lists adminer. The profile is only a selector. The service definitions stay in the file, which means the file still parses and validates in full even when a profile is off.
Choosing profile names
Name profiles after a purpose, such as debug, tools, or monitoring, and not after an environment. A name like dev suggests it controls the whole environment, which is the job of override files. A purpose-based name works across environments. Developers and CI can both use tools for the same migration job.
Exercise
A compose.yaml defines web (no profile), db (no profile), and adminer (profiles: [debug]). You run docker compose up -d with no flags. Which services start, and what would happen if web declared depends_on: [adminer]?
Show answer
Only web and db start, because adminer has an inactive profile. If web depended on adminer, Compose would report that the dependency is not enabled and fail, because a default service cannot rely on a service that is switched off. Remove that dependency or move adminer into the default set.