TExcelTAD
Home › Docker Compose › Profiles

Profiles

Not every service should start every time. A database admin UI, a mail catcher, a load generator, a one-off migration job, a seeding script: useful sometimes, noise the rest of the time. You could move them into separate files, but then they lose the shared networks and variables, and you need to remember which -f flags to pass.

Profiles keep optional services in the main file and switch them on by name. The Profiles & Override Files lesson in the Docker course introduced the idea; here we look at the exact rules and the edge cases.

The rule

  • A service without profiles is always enabled.
  • A service with profiles: [a, b] is enabled only when at least one of its profiles is active.
  • Profiles are activated with --profile NAME (repeatable) or the COMPOSE_PROFILES variable (comma-separated), and * activates all of them.
compose · try it yourself
# $ docker compose config --profiles
# $ docker compose config --services
# $ docker compose --profile debug config --services
# $ docker compose --profile debug --profile tools config --services
# $ docker compose --profile "*" config --services
services:
  api:
    image: ghcr.io/acme/api:3.1
  db:
    image: postgres:17
  adminer:
    image: adminer:5
    profiles: [debug, tools]
  mailpit:
    image: axllent/mailpit:v1.27
    profiles: [debug]
  migrate:
    image: ghcr.io/acme/api:3.1
    command: ["alembic", "upgrade", "head"]
    profiles: [tools]
$ docker compose config --profiles
debug
tools
$ docker compose config --services
api
db
$ docker compose --profile debug config --services
adminer
api
db
mailpit
$ docker compose --profile debug --profile tools config --services
adminer
api
db
mailpit
migrate
$ docker compose --profile "*" config --services
adminer
api
db
mailpit
migrate

adminer belongs to two profiles, so either one enables it. --profiles lists every profile name that appears in the file, which is handy for shell completion or a README.

COMPOSE_PROFILES

Typing --profile on every command is tedious and easy to forget halfway (up --profile debug but then down without it leaves the debug containers running). Put the profiles a machine needs in .env instead:

compose · try it yourself
# $ docker compose config --services
services:
  api:
    image: ghcr.io/acme/api:3.1
  cache:
    image: redis:8
    profiles: [cache]
  grafana:
    image: grafana/grafana:12.1.0
    profiles: [monitoring]

# --- .env
COMPOSE_PROFILES=cache,monitoring
api
cache
grafana

Explicitly named services

Profiles only filter the services Compose picks by itself. When you name a service on the command line, Compose enables it, whatever its profiles. That's what makes profiles perfect for one-off jobs:

bash
$ docker compose run --rm migrate          # runs even though "tools" isn't active
$ docker compose up -d adminer             # starts adminer (and its dependencies)
$ docker compose up -d                     # starts api and db only, as before

Dependencies on profiled services

A profiled service may depend on always-on services (migrate → db): starting migrate starts db too. The other direction is a trap. If an always-on service depends on a service that is switched off, the project is invalid:

compose · try it yourself
services:
  api:
    image: ghcr.io/acme/api:3.1
    depends_on: [cache]
  cache:
    image: redis:8
    profiles: [cache]
service "api" depends on undefined service "cache": invalid compose project

If the dependency really is optional ("use Redis when it's there"), say so with required: false. Compose then accepts the file: the dependency stays listed, but because it is not required it is simply skipped while cache is disabled:

compose · try it yourself
services:
  api:
    image: ghcr.io/acme/api:3.1
    depends_on:
      cache:
        condition: service_started
        required: false
  cache:
    image: redis:8
    profiles: [cache]
name: demo
services:
  api:
    image: ghcr.io/acme/api:3.1
    networks:
      default: null
    depends_on:
      cache:
        condition: service_started
        required: false
networks:
  default:
    name: demo_default

The app must, of course, cope with Redis being absent.

A model of service selection

python · try it yourself
services = {
    "api":     {"profiles": [], "depends_on": ["db"]},
    "db":      {"profiles": [], "depends_on": []},
    "adminer": {"profiles": ["debug", "tools"], "depends_on": ["db"]},
    "mailpit": {"profiles": ["debug"], "depends_on": []},
    "migrate": {"profiles": ["tools"], "depends_on": ["db"]},
}

def selected(active, named=()):
    on = set()
    for name, svc in services.items():
        if not svc["profiles"] or "*" in active or set(svc["profiles"]) & set(active):
            on.add(name)
    if named:                                  # `up adminer`, `run migrate`
        todo, on = list(named), set()
        while todo:                            # a named service + its dependencies
            name = todo.pop()
            if name not in on:
                on.add(name)
                todo += services[name]["depends_on"]
    return sorted(on)

print("up                     ->", selected([]))
print("--profile debug up     ->", selected(["debug"]))
print("COMPOSE_PROFILES=*     ->", selected(["*"]))
print("run migrate            ->", selected([], named=["migrate"]))
print("up mailpit             ->", selected([], named=["mailpit"]))
up                     -> ['api', 'db']
--profile debug up     -> ['adminer', 'api', 'db', 'mailpit']
COMPOSE_PROFILES=*     -> ['adminer', 'api', 'db', 'mailpit', 'migrate']
run migrate            -> ['db', 'migrate']
up mailpit             -> ['mailpit']

Good uses (and bad ones)

Good profile Why
debug: adminer, mailpit, a profiler developer tools nobody needs in CI
tools: migrate, seed, backup one-off jobs you run on demand
monitoring: Prometheus, Grafana heavy, only sometimes needed locally
e2e: browser test runner only in the end-to-end CI job

Avoid using profiles as an environment switch (dev vs prod versions of the same service, api-dev and api-prod). Two near-identical services drift apart, and a typo starts both. Different settings for the same service belong in override files. Profiles answer "which services?", overrides answer "configured how?".

Note: docker compose down only stops services that are enabled for the current command. If you started --profile debug, use docker compose --profile debug down (or --profile "*") to remove everything, or put the profiles in COMPOSE_PROFILES so every command agrees.

Swarm has no profiles: docker stack config rejects the key (services.<name>: additional property 'profiles' is not allowed). Kubernetes tools solve the same problem with separate manifests, Kustomize overlays or Helm values (enabled: false).

Learn more: Profiles & Override Files · Override Files & Merging · A Development Workflow

Test yourself

A service has no profiles key. When does it start?

  1. Only with --profile default
  2. Always
  3. Never unless named
  4. Only when COMPOSE_PROFILES is empty

migrate has profiles: [tools]. What does docker compose run --rm migrate do when no profile is active?

  1. Fails: the service is disabled
  2. Runs migrate, because naming a service enables it
  3. Starts every service
  4. Asks for a profile

api (no profile) has depends_on: [cache] and cache is in profile cache. What happens without the profile?

  1. cache starts anyway
  2. api starts without cache
  3. The project is invalid: api depends on an undefined service
  4. Compose asks which to start