log in
consulting hosting industries the daily tools about contact

Docker Compose Profiles: One File, Three Environments, No Chaos

Docker Compose profiles let you keep dev, test, and prod services in a single file and activate only what you need. Here's how I actually use them.

I used to maintain three separate Compose files - docker-compose.yml, docker-compose.test.yml, and something hacked together for prod-like staging. It worked until it didn't. Services drifted, environment variables got out of sync, and I'd waste an hour debugging something that turned out to be a volume mount that only existed in one file. Compose profiles fix most of that, and I wish I'd reached for them sooner.

What Problem Profiles Actually Solve

Docker Compose has always let you override files with -f. That works, but it means you're maintaining multiple documents that share most of their content. Change a network name in one and forget the other two, and you've got a fun Friday afternoon ahead of you.

Profiles give you a single docker-compose.yml where every service has an optional profiles: key. Services without that key run always. Services with a profile only run when you explicitly activate that profile with --profile on the command line or by setting COMPOSE_PROFILES in your environment.

The mental model is simple: your app server, your database, your cache - those have no profile, they always come up. Your Mailpit container for local email testing gets profiles: [dev]. Your dedicated test database with its own volume gets profiles: [test]. Your Cloudwatch log forwarder or your production health-check sidecar gets profiles: [prod]. One file, clean targeting.

A Real Working Example

Here's a condensed version of the Compose file I landed on for a Laravel application I run for a healthcare client. I've simplified the specifics but the structure is real.

# docker-compose.yml

services:

  app:
    build:
      context: .
      target: app
    env_file: .env
    volumes:
      - .:/var/www/html
    depends_on:
      db:
        condition: service_healthy
    networks:
      - internal

  db:
    image: postgres:16
    environment:
      POSTGRES_DB: ${DB_DATABASE}
      POSTGRES_USER: ${DB_USERNAME}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    volumes:
      - db_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USERNAME}"]
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - internal

  redis:
    image: redis:7-alpine
    networks:
      - internal

  # - - - DEV ONLY - - -

  mailpit:
    image: axllent/mailpit
    profiles: [dev]
    ports:
      - "8025:8025"
      - "1025:1025"
    networks:
      - internal

  horizon-dev:
    build:
      context: .
      target: app
    command: php artisan horizon
    profiles: [dev]
    env_file: .env
    volumes:
      - .:/var/www/html
    depends_on:
      - redis
    networks:
      - internal

  # - - - TEST ONLY - - -

  db_test:
    image: postgres:16
    profiles: [test]
    environment:
      POSTGRES_DB: testing
      POSTGRES_USER: testing
      POSTGRES_PASSWORD: testing
    tmpfs:
      - /var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U testing"]
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - internal

  # - - - PROD ONLY - - -

  log-forwarder:
    image: amazon/cloudwatch-agent:latest
    profiles: [prod]
    env_file: .env.prod
    volumes:
      - /var/log/app:/var/log/app:ro
      - ./infra/cloudwatch.json:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json:ro
    networks:
      - internal

volumes:
  db_data:

networks:
  internal:

Now here's how I actually invoke things from the command line or from CI:

# Local development - bring up everything a dev needs
COMPOSE_PROFILES=dev docker compose up -d

# Run the test suite with an isolated in-memory test db
COMPOSE_PROFILES=test docker compose up -d db_test
COMPOSE_PROFILES=test docker compose exec app php artisan test --env=testing

# Or in CI, one liner
docker compose --profile test run --rm app php artisan test --env=testing

# Prod deploy (assuming you're using Compose on a VPS, which I do for smaller clients)
COMPOSE_PROFILES=prod docker compose up -d

And in my Laravel .env.testing I point DB_HOST=db_test and DB_DATABASE=testing. The test database runs on tmpfs so it's fast and ephemeral - no leftover state between runs.

The Gotchas That Will Bite You

Services with profiles won't start as dependencies. This is the one that trips people up first. If app has depends_on: db_test and db_test has profiles: [test], running without the test profile won't start db_test - it'll just error that the dependency isn't available. Keep your always-on services dependency-free from profiled ones, or only create those depends_on relationships inside profiled services.

docker compose up with no profile still starts profiled services if you name them explicitly. Run docker compose up db_test and it will start regardless of COMPOSE_PROFILES. That's actually useful sometimes - but it's also surprising behavior when a junior dev on the project wonders why their email testing container isn't running and starts poking around and manually starts things.

Volume conflicts between profiles are your problem. If your dev database and your test database both tried to use the same named volume, you'd be blowing away dev data every time you ran tests. I solve this by giving the test DB a tmpfs mount instead of a named volume. Fast, isolated, no collisions.

COMPOSE_PROFILES in your shell is sticky. If you export COMPOSE_PROFILES=dev in your shell session and then try to run something that shouldn't have dev services, you'll be confused when Mailpit shows up. I never export it in my shell. I either prefix the command inline or set it in a .env file at the project root - and I add .env to .gitignore so each developer sets their own. For CI, it's an explicit environment variable in the pipeline config.

Profile names are freeform strings with no validation. Typo profiles: [devv] and Compose won't warn you. The service just never comes up and you spend ten minutes wondering why. I keep a comment block at the top of every Compose file listing the valid profiles and what they're for. Low-tech, but it's saved me more than once.

A Wrinkle with extends and Profiles

If you use Compose's extends keyword to share base service configs (which I do for the app service and anything that derives from it), profiles apply to the final service definition, not the base. That means you can define a base app block without a profile and then extend it into a profiled horizon-dev block with no issue. That's the right pattern. Don't put a profile on the thing you're extending from - put it on the extender.

When I Reach for Profiles (and When I Don't)

I reach for profiles whenever a project has more than one developer or more than one environment that shares infrastructure. Which is almost every project.

Specifically, profiles earn their keep when:

  • I want to run the full test suite in CI without the dev convenience containers eating resources or port-conflicting with each other.
  • A client's staging server and production server both run Compose (common for smaller deployments under a managed hosting arrangement), and I want one file I can ship.
  • A project has optional services - say, a PDF rendering container or a WebSocket server that only some features need - and I don't want every developer starting them by default.

I don't use profiles when the environments are genuinely different enough to warrant separate files. If your prod deployment is Kubernetes and your local is Compose, profiles won't help you - you need a different tool chain. Profiles are for the case where the shape of the deployment is the same but the roster of services changes.

I also don't use profiles as a substitute for proper secrets management. The env_file: .env.prod on that log forwarder above is a convenience - in a real production environment with any sensitivity (and healthcare clients definitely count), those credentials live in something like AWS Secrets Manager or Doppler, not a file on disk. The Compose file just tells you where the slot is.

The Payoff

Maintaining one file is not just tidier - it means when I update the internal network name or change the base image, it changes everywhere at once. The drift problem that made me lose a Friday afternoon is gone.

If you're still juggling multiple Compose files with -f flags, profiles are worth an afternoon to migrate. It's one of those features that feels obvious in retrospect and I'm mildly annoyed it took me as long as it did to actually use it properly.

Related

Need help shipping something like this? Get in touch.