Docker cookbook · Beginner
Start optional tools only when needed with Compose profiles
Keep admin UIs, mail catchers and debug tools out of the default stack but one flag away.
Approach
Put optional services in a profile. They start only with --profile name (or COMPOSE_PROFILES).
Solution
services:
api:
build: .
db:
image: postgres:18-alpine
environment: {POSTGRES_PASSWORD: dev}
adminer:
image: adminer:5
ports: ["127.0.0.1:8081:8080"]
profiles: [tools]
mailpit:
image: axllent/mailpit:v1.27
ports: ["127.0.0.1:8025:8025"]
profiles: [tools]
docker compose --profile tools up -d
Pitfalls
- A default service depending on a profiled one: Compose can't start it without the profile.
- Forgetting
--profileondown, leaving profiled containers running.
Variations
- Several profiles per service (
profiles: [tools, debug]). COMPOSE_PROFILES=toolsin.envfor developers who always want them.
Further reading: Compose profiles
Reference: https://docs.docker.com/compose/how-tos/profiles/
More Docker recipes
- Add a healthcheck to an HTTP service
- Build a layered image for a Spring Boot app
- Build a production image for a Node.js service
- Build a production image for a Python web app
- Build, cache and push images in GitHub Actions
- Build one image tag for amd64 and arm64
- Configure nginx from environment variables
- Develop with Compose and live reload