Docker Compose Profiles and Override Trick: One Compose File for Multiple Environments

One project, three conflicting needs. On a laptop, you want MailHog catching test emails and a debug port open. On production, both must vanish: no debug port, no exposed database panel. Copying compose.yaml into three files feels fast, but every core change then repeats three times, and one stale copy eventually breeds a hard-to-trace bug.

Compose ships two built-in mechanisms that close that gap: profiles choose which services run, overrides change configuration per environment.

What is a Docker Compose profile?

A profile is a label attached to a service in compose.yaml. The rule is simple and comes straight from Docker's documentation: a service with no profiles always starts when you run docker compose up. A service with a profile only starts when that profile is active.

You activate a profile with the --profile flag or the COMPOSE_PROFILES variable:

docker compose --profile dev up
COMPOSE_PROFILES=dev docker compose up

Several profiles can be active at once — --profile dev --profile debug or COMPOSE_PROFILES=dev,debug — and --profile "*" enables all of them. Two behaviors regularly surprise newcomers. First, core services should not carry a profile, so no up ever forgets to start the database. Second, a service you name explicitly on the command line still runs even when its profile is inactive, together with its depends_on dependencies — handy for one-off tools such as a database migration, without switching on the whole profile.

Profile names may only use letters, digits, underscores, dots, and hyphens. As long as you hold on to this rule — no profile means always on, a profile means on only when asked — Compose stays predictable across every environment.

1. Mark optional services with profiles

A service without profiles is always active. A service with profiles only runs when one of those profiles is enabled. Keep this pattern in compose.yaml:

services:
  app:
    image: myapp:latest
  db:
    image: postgres:16
  mailhog:
    image: mailhog/mailhog
    ports: ["1025:1025", "8025:8025"]
    profiles: [dev]
  db-admin:
    image: adminer
    ports: ["8080:8080"]
    profiles: [dev]

Start the dev profile:

docker compose --profile dev up -d

Expect app, db, mailhog, and db-admin to run; without the flag, only app and db start. The variable form fits a deployment script:

COMPOSE_PROFILES=dev docker compose up -d
COMPOSE_PROFILES=dev,debug docker compose up -d

Targeting a service by name also starts it even when its profile is off: docker compose up mailhog runs MailHog alone, plus its dependencies. For the whole dev set, enable the profile.

Two pitfalls are worth knowing. First, a profiled service that "refuses to start" with no error: nothing is broken, Compose ignores it until its profile is enabled. Check with docker compose config --services once the profile is on. Second, a depends_on pointing at an inactive profiled service: Compose rejects that model. Give the dependency the same profile, or leave core services profile-free.

depends_on: start order does not mean ready

Profiles decide which services run; depends_on decides the order they run in. Compose always creates and removes containers in dependency order — db is created before web when web depends on db. But one misunderstanding breaks more first deployments than anything else: Compose only waits until a dependency container is running, not ready. A database whose container is up is not necessarily accepting connections yet; an app that tries to log in right away can fail before the database has even opened its port.

To wait for a ready state, use condition with one of these three options:

  • service_started — the default behaviour: continue once the dependency container is running. The same as writing depends_on with no condition.
  • service_healthy — continue only after the dependency's healthcheck passes. This is the right choice for databases and anything else that needs warm-up time.
  • service_completed_successfully — continue only after the dependency has run to a successful completion. Made for one-shot services such as migrations or initialisation.

The most common example, a web app waiting for Postgres to actually be ready:

services: 
  db: 
      image: postgres:16 
          environment: 
                POSTGRES_USER: app 
                      POSTGRES_PASSWORD: secret 
                            POSTGRES_DB: appdb 
                                healthcheck: 
                                  test: ["CMD-SHELL", "pg_isready -U app -d appdb"] 
                                        interval: 10s 
                                        retries: 5 
                                    web: 
                                    image: myapp:latest 
                                    depends_on: 
                                      db: 
                                        condition: service_healthy

Compose waits for the db healthcheck to pass — checked every 10 seconds, up to 5 attempts — before creating web. Without condition: service_healthy, web is created as soon as the db container is running, whether Postgres is ready or not.

Two related behaviours are worth remembering. First, restart: true on depends_on restarts the dependent service automatically when its dependency is restarted through a Compose operation such as docker compose restart — useful so the app reconnects to a freshly restarted database. Second, Compose also removes services in dependency order: web is removed before db. And remember the trap from the previous section: a depends_on pointing at a profiled service whose profile is inactive is still rejected by Compose — give the dependency the same profile, or leave core services profile-free.

2. Reshape configuration per environment with overrides

Profiles answer "which services run"; overrides answer "what changes". By default Compose reads compose.yaml then compose.override.yaml from the same directory, no flags needed — the right home for local tweaks:

# compose.override.yaml
services:
  app:
    ports: ["3000:3000", "9229:9229"]
    volumes: ["./src:/app/src"]
    environment: [NODE_ENV=development]

Run normally; the override loads automatically:

docker compose up -d

For production, create compose.prod.yaml and name both files explicitly:

docker compose -f compose.yaml -f compose.prod.yaml up -d

Two consequences: first, compose.override.yaml is not loaded, since you supplied your own file list — dev configuration cannot leak onto the server. Second, the file named last wins, so the production file must always come last.

The merge rules: single-value fields like image and command are fully replaced by the later file. Multi-value fields like ports are combined, not replaced. environment, labels, and volumes merge by key or mount path, later file winning ties. So never put a debug port in the base file; keep it in the dev override.

Path trap: relative paths in an override resolve against the first base file, not the override itself — keep all files in one directory.

Inspect the merged result before up:

docker compose -f compose.yaml -f compose.prod.yaml config

If a debug port still shows up there, a file is out of order — caught before any container is created.

Summary

MechanismUsed for
ProfilesChoosing optional services
depends_onStart order + readiness condition
COMPOSE_PROFILESProfiles without flags
compose.override.yamlDev tweaks, auto-loaded
-f flagCombining files per environment
configChecking the merge

Final checklist

  • Core services (app, database) carry no profile, so they always run.
  • MailHog and the database panel start only via the dev profile.
  • Debug ports live only in compose.override.yaml, never in the base file.
  • Production uses explicit -f flags, production file last.
  • Merged output checked with docker compose config before up.
  • depends_on uses the right condition (service_healthy for a database, not just service_started).

Which would you fix first: duplicated compose files, or debug ports shipping to production? Tell us in the comments, plus the dev service you use most via a profile.

Read also: Watchtower Not Updating Containers? Fix Docker Auto-Updates That Seem Stuck to keep the services your Compose file runs up to date, and Gitea, a lightweight self-hosted Git server.

Diskusi

Ada pertanyaan atau pengalaman yang ingin dibagikan?

0komentar

Komentar ditinjau sebelum ditampilkan. Mari berdiskusi dengan sopan dan sesuai topik.