Every project eventually needs more than one stack: a development setup with hot reload and debug tools, a test setup with a throwaway database, and a production setup that is lean and locked down. Maintaining three separate Compose files duplicates everything and lets them drift apart. Docker Compose profiles solve this by keeping one file and tagging each service with the situations it belongs to, so you start exactly the services you need with one flag.

This guide explains Docker Compose profiles from scratch: how to define them, start and stop profile services, combine profiles with overrides, and avoid the common traps. If you already use Compose healthchecks, see our Compose healthchecks guide, and for storage choices behind these stacks, our volumes versus bind mounts guide.
Prerequisites
- Docker Engine and the Docker Compose plugin installed, verified with
docker compose version. - A project directory with a
compose.yamlfile. The examples below use a web app, a database, and optional tooling services. - Basic familiarity with Compose services, networks, and volumes.
Docker Compose Profiles Basics
A profile is a label on a service. Services without a profile always start with docker compose up. Services with a profile start only when that profile is active, either through the --profile flag or the COMPOSE_PROFILES environment variable. This example keeps the core app and database always on, adds a mail catcher for development, and adds a backup job for production:
services:
web:
image: myapp:latest
ports:
- "8080:80"
depends_on:
- db
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: devpassword
volumes:
- pgdata:/var/lib/postgresql/data
mailhog:
image: mailhog/mailhog:latest
profiles: ["dev"]
ports:
- "1025:1025"
- "8025:8025"
backup:
image: postgres:16
profiles: ["prod"]
volumes:
- pgdata:/var/lib/postgresql/data
command: pg_dumpall -U postgres
volumes:
pgdata:
Running docker compose up -d starts only web and db. Add --profile dev to include MailHog, or --profile prod to include the backup job. A service can belong to several profiles by listing them, for example profiles: ["dev", "test"].
docker compose up -d
docker compose --profile dev up -d
docker compose --profile dev --profile test up -d
COMPOSE_PROFILES=dev docker compose up -d
Docker Compose Profiles for Dev, Test, and Production
The cleanest pattern is to put shared configuration in the base file and use profiles for the differences, not for whole alternative stacks. Development profiles typically add debuggers, mail catchers, admin panels, and volume mounts for live code. Test profiles add a test database, a seed job, or a test runner that exits after the suite. Production profiles add backup jobs, log shippers, and monitoring exporters that you would never run on a laptop.
services:
web:
image: myapp:latest
adminer:
image: adminer:latest
profiles: ["dev"]
ports:
- "8081:8080"
seed:
image: myapp:latest
profiles: ["test"]
command: python seed.py
depends_on:
- db
node-exporter:
image: prom/node-exporter:latest
profiles: ["prod"]
ports:
- "9100:9100"
Notice that profile services still join the same project network, so the admin panel can reach the database by service name. Profiles control whether a service runs, not whether it can talk to the rest of the stack.
Combining Profiles with Dependencies and Overrides
Dependencies interact with profiles in a way that surprises people. If an always-on service depends on a profile service, Compose will start that dependency even when its profile is inactive, because the dependency is required. Conversely, if a profile service depends on another service, that dependency starts too. Design dependencies so that optional tooling depends on core services, never the reverse.
For environment-specific values, combine profiles with an override file rather than duplicating services. Keep compose.yaml as the shared base, put developer conveniences in compose.dev.yaml under a dev profile or as an override, and merge files explicitly so the result is predictable:
docker compose -f compose.yaml -f compose.dev.yaml --profile dev up -d
docker compose config
The docker compose config command renders the merged configuration, including which services each profile activates. Run it whenever a stack does not start the way you expect; it shows the effective file Compose actually uses, which is far more useful than re-reading the source files.
Stopping, Listing, and Inspecting Profile Services
Stopping follows the same rule as starting: without the profile flag, Compose stops only the services it knows about in the current invocation. To stop everything including profile services, pass the profile or use down, which removes all project containers regardless of profile:
docker compose --profile dev down
docker compose down
docker compose ps
docker compose --profile dev ps
Use docker compose ps with the profile to confirm an optional service is running, and docker compose logs mailhog to inspect it by name. If a service never appears, check its profile spelling: profile names are case-sensitive, and a typo silently means the service is never selected.
Troubleshooting Docker Compose Profiles
A profile service never starts. Confirm the flag is spelled exactly like the profile, and that you passed it before the up subcommand or set COMPOSE_PROFILES. A flag placed after up is ignored or treated as a service name.
Everything starts even without a profile. Check whether the service really has a profile, and whether an always-on service depends on it. Also check for an old COMPOSE_PROFILES variable in your shell or .env file that activates profiles silently.
A profile service cannot reach the database. Profiles do not change networking, so the cause is usually a missing dependency, a wrong service name, or a healthcheck that has not passed yet. Add depends_on with a health condition for database-backed tooling.
Production accidentally runs a dev tool. Never put a dev profile service behind a port published on a public interface, and keep production commands in CI explicit: the deployment script should list exactly which profiles it activates, with no environment variable fallback.
Docker Compose Profiles Best Practices
Keep profiles few and meaningful: dev, test, and prod cover most projects, with maybe one extra for local tooling. Give every profile service a clear purpose in a comment, keep secrets out of the Compose file by using environment files or a secrets manager, and document the standard commands in the project README so a new developer runs docker compose --profile dev up -d instead of guessing.
Docker Compose profiles turn one Compose file into a flexible toolkit: the same definition serves a laptop, a CI runner, and a server, with each environment running only what it needs. Start by tagging your optional tooling, test the dev and prod combinations explicitly, and let docker compose config confirm the result. For the official reference, see the Docker Compose profiles documentation.