Optimize Docker Compose Workflows with Profiles, Extends, and Depends_on
These articles are AI-generated summaries. Please check the original sources for full details.
Docker Compose: Speed Up Your Workflow with Profiles, Extends, and Depends_on
Docker Compose configurations often become bloated as projects scale, leading to wasted resources and startup race conditions. This technical overview demonstrates how to utilize profiles to toggle non-essential services like mailhog or tests_runner only when needed.
Why This Matters
In complex microservices architectures, the ideal model of a flat container list fails when services like APIs attempt to connect to databases before they are ready. Implementing robust orchestration via healthchecks and configuration inheritance prevents the ‘it works on my machine’ syndrome while reducing maintenance overhead across engineering teams.
Key Insights
- Profiles allow selective service activation; services without a profile start by default while others like ‘test’ require explicit flags.
- The ‘extends’ directive and YAML anchors enable service inheritance to follow DRY (Don’t Repeat Yourself) principles.
- The long syntax of ‘depends_on’ supports ‘service_healthy’ conditions to ensure dependencies are fully operational before dependent services start.
- Healthcheck tests such as ‘mysqladmin ping’ or ‘pg_isready’ are essential for accurate container readiness signaling.
- Using ‘docker compose —profile ”*” up’ allows developers to start all defined services across all profiles simultaneously for full-stack testing.
Working Examples
Example of using profiles to isolate testing and development tools.
services:
app:
image: my-application:1.0
tests_runner:
image: my-tests:latest
profiles: ["test"]
mailhog:
image: mailhog/mailhog
profiles: ["devtools"]
Using YAML anchors to share common configuration between web and worker services.
x-base-service: &common_config
image: my-application:1.0
volumes:
- .:/app
environment:
- DATABASE_URL=postgres://user:password@db:5432/mydatabase
services:
web:
<<: *common_config
command: npm start
worker:
<<: *common_config
command: npm run worker
Orchestrating startup order using the long-syntax depends_on with service healthchecks.
services:
api:
image: my-api:latest
depends_on:
db:
condition: service_healthy
db:
image: mysql:8.0
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 5s
timeout: 5s
retries: 5
Practical Applications
- Use Case: Integrating a ‘mailhog’ service for email testing only during the ‘devtools’ profile to save local system resources. Pitfall: Placing essential containers inside a profile, which prevents them from starting by default.
- Use Case: Defining a base configuration for ‘web’ and ‘worker’ services using YAML anchors to ensure identical environment variables. Pitfall: Forgetting to update shared anchors when changing credentials, causing out-of-sync service states.
- Use Case: Using ‘depends_on’ with ‘service_healthy’ for a MySQL database to prevent API connection failures during cold starts. Pitfall: Using ‘depends_on’ without a defined healthcheck, which only waits for the container process to start.
References:
Continue reading
Next article
Edge Computing vs. Cloud LLMs: ROI Analysis for Enterprises
Related Content
Mastering Multi-Service Orchestration with Docker Compose
Optimize local development environments using Docker Compose 3.8 to orchestrate web, Postgres 15, and Redis services with automated scaling.
Simplify VPS Management: Deploying via SSH with sshship
Streamline solo developer workflows by connecting Linux VPS servers over SSH to automate Git deployments, monitoring, and S3-compatible backups.
Scalable Infrastructure for Digital Assets: Deployment Strategies with Hostinger
Developer Siddharth manages a diverse portfolio of SEO tools, game portals, and Android app backends using a consolidated Hostinger infrastructure.