How to Use Docker Compose Profiles and Multiple Environments

Docker Compose profiles let you selectively enable/disable services within a single compose file — useful for managing optional services or different environment configurations without maintaining entirely separate compose files.

The Problem Profiles Solve

Not every service in a compose file needs to run in every context — a debugging tool, an optional admin interface, or a service only needed in development shouldn't necessarily start with your core production stack every time.

Defining Profiles

services:
  app:
    image: myapp:latest
    ports:
      - "3000:3000"

  db:
    image: postgres:16

  adminer:
    image: adminer
    ports:
      - "8080:8080"
    profiles:
      - debug

  mailhog:
    image: mailhog/mailhog
    ports:
      - "8025:8025"
    profiles:
      - debug

Services without a profiles key always start; services with a profile only start when that profile is explicitly activated.

Starting Without Any Profile (Core Services Only)

docker compose up -d

Only app and db start — adminer and mailhog remain inactive.

Starting With a Specific Profile Activated

docker compose --profile debug up -d

Now all services start, including the debug-profiled ones — useful for local development where you want database admin tools and email testing utilities, but not in production.

Activating Multiple Profiles

docker compose --profile debug --profile monitoring up -d

Setting a Default Profile via Environment Variable

export COMPOSE_PROFILES=debug
docker compose up -d

Avoids needing to type --profile every time during a development session.

Alternative Approach: Override Files for Environment Differences

See Docker Compose Override Files: Managing Dev vs Prod Configs for a complementary technique — profiles control which services run, while override files handle broader configuration differences (environment variables, resource limits) between environments; often used together.

Practical Use Case: Development-Only Debugging Tools

services:
  redis-commander:
    image: rediscommander/redis-commander
    profiles:
      - dev
    ports:
      - "8081:8081"

A Redis admin GUI useful during development but never needed (or wanted) in production — profiles cleanly exclude it from production deployments while keeping it available for local use.

Practical Use Case: Optional Heavy Services

Services with significant resource requirements that aren't always needed (a full monitoring stack, a heavy analytics service) can be profile-gated, letting developers opt in only when actually working on something requiring them.

Verifying Which Services Are Active

docker compose ps

Common Errors

Expected service doesn't start — verify you're activating the correct profile name, and check for typos between the profile defined in the compose file and the one specified on the command line.

Continue Reading

Browse more articles in Docker & Containers.

  • docker compose profiles, docker compose multiple environments, compose profile activation, docker dev environment
  • 0 Пользователи нашли это полезным
Помог ли вам данный ответ?

Связанные статьи

How to Install Docker Engine on Ubuntu & Debian (Complete Guide)

Docker lets you package applications and their dependencies into lightweight, isolated containers...

How to Install Docker Compose on Ubuntu & Debian

Docker Compose lets you define and run multi-container applications from a single YAML file,...

Deploy Your First Docker Container: A Beginner's Walkthrough

With Docker installed, this hands-on walkthrough covers the core workflow you'll use constantly:...

How to Update Docker and Docker Compose Safely

Keeping Docker Engine and Compose updated brings security fixes, performance improvements, and...

Docker Compose Troubleshooting: Common Errors & Fixes

A reference guide to the Docker and Docker Compose errors you're most likely to encounter, with...