Docker Compose — Profile Use Cases
Profiles shine in real-world scenarios where different team members or environments need different sets of services.
Use Case 1: Development Tools
services:
api:
build: ./api
ports: ["3000:3000"]
db:
image: postgres:16
# Dev-only tools
adminer:
image: adminer
ports: ["8080:8080"]
profiles: [dev]
mailhog:
image: mailhog/mailhog
ports: ["1025:1025", "8025:8025"]
profiles: [dev]
Developers run --profile dev; production skips dev tools.
Use Case 2: Testing
services:
api:
build: ./api
db:
image: postgres:16
# Test runner
test:
build: ./api
command: npm test
profiles: [test]
depends_on:
db:
condition: service_healthy
docker compose --profile test run --rm test
Use Case 3: Monitoring Stack
services:
api:
build: ./api
prometheus:
image: prom/prometheus
profiles: [monitoring]
grafana:
image: grafana/grafana
profiles: [monitoring]
alertmanager:
image: prom/alertmanager
profiles: [monitoring]
Use Case 4: Database Choice
services:
api:
build: ./api
postgres:
image: postgres:16
profiles: [postgres]
mysql:
image: mysql:8
profiles: [mysql]
mongo:
image: mongo:7
profiles: [mongo]
docker compose --profile postgres up -d # Use PostgreSQL
docker compose --profile mysql up -d # Use MySQL
Use Case 5: CI/CD Pipeline
services:
api:
build: ./api
lint:
build: ./api
command: npm run lint
profiles: [ci]
test:
build: ./api
command: npm test
profiles: [ci]
e2e:
build: ./e2e
profiles: [ci, e2e]
# CI runs all checks
docker compose --profile ci up --abort-on-container-exit
Summary
| Use Case | Profile Name | Services |
|---|---|---|
| Dev tools | dev |
adminer, mailhog |
| Testing | test |
test runner |
| Monitoring | monitoring |
prometheus, grafana |
| DB choice | postgres/mysql |
Selected database |
| CI/CD | ci |
lint, test, e2e |
Quick Reference
| Item | Detail |
|---|---|
| Dev tools | –profile dev |
| Testing | –profile test run –rm test |
| Monitoring | –profile monitoring |
| DB choice | –profile postgres or –profile mysql |
| CI/CD | –profile ci up –abort-on-container-exit |