Docker
Docker Compose V2: The Complete Guide to Modern Container Orchestration
Master Docker Compose V2 with this comprehensive guide covering new features, best practices, and advanced orchestration patterns for modern containerized applications
March 15, 2024
DevHub Team
4 min read
Docker Compose V2: The Complete Guide to Modern Container Orchestration
Docker Compose V2 brings significant improvements to container orchestration with enhanced features, better performance, and improved developer experience. This guide explores everything you need to know about Docker Compose V2.
Architecture Overview
graph TB
subgraph "Compose V2"
A["CLI Interface"]
B["Compose Spec"]
C["Docker API"]
end
subgraph "Features"
D["Dependency Resolution"]
E["Network Management"]
F["Volume Management"]
G["Service Discovery"]
end
subgraph "Extensions"
H["Profiles"]
I["Include"]
J["Deploy"]
end
A --> B
B --> C
B --> D
B --> E
B --> F
B --> G
B --> H
B --> I
B --> J
classDef compose fill:#1a73e8,stroke:#fff,color:#fff
class A,B,C,D,E,F,G,H,I,J compose
Key Features
Feature | Description | Benefits |
---|---|---|
Compose Spec | Standardized format | Portability |
Profiles | Environment grouping | Flexibility |
Include | Composition reuse | Modularity |
Basic Configuration
Service Definition
# docker-compose.yml services: web: build: context: ./web dockerfile: Dockerfile args: NODE_ENV: development ports: - "3000:3000" environment: - DATABASE_URL=postgres://user:pass@db:5432/dbname depends_on: db: condition: service_healthy deploy: replicas: 2 resources: limits: cpus: '0.50' memory: 512M db: image: postgres:14-alpine volumes: - db_data:/var/lib/postgresql/data environment: - POSTGRES_USER=user - POSTGRES_PASSWORD=pass - POSTGRES_DB=dbname healthcheck: test: ["CMD-SHELL", "pg_isready -U user -d dbname"] interval: 5s timeout: 5s retries: 5 volumes: db_data:
Profile Configuration
# docker-compose.yml services: web: profiles: ["web", "frontend"] # ... service configuration ... api: profiles: ["api", "backend"] # ... service configuration ... db: profiles: ["db", "backend"] # ... service configuration ... redis: profiles: ["cache", "backend"] # ... service configuration ...
Advanced Features
Include Directive
# base.yml services: base: image: nginx:alpine volumes: - ./nginx.conf:/etc/nginx/nginx.conf # docker-compose.yml include: - base.yml services: web: extends: file: base.yml service: base environment: - VIRTUAL_HOST=example.com
Dependency Management
# docker-compose.yml services: app: build: . depends_on: db: condition: service_healthy restart: true redis: condition: service_started migration: condition: service_completed_successfully migration: image: flyway command: migrate depends_on: db: condition: service_healthy
Development Workflows
Multi-Environment Setup
# docker-compose.yml services: web: build: context: . target: ${TARGET:-development} volumes: - .:/app - /app/node_modules environment: - NODE_ENV=${NODE_ENV:-development} command: ${COMMAND:-npm run dev} profiles: - dev - prod # docker-compose.override.yml services: web: ports: - "3000:3000" volumes: - .:/app:cached environment: - DEBUG=true
Testing Configuration
Environment | Command | Profile |
---|---|---|
Development | compose up | dev |
Testing | compose --profile test up | test |
Production | compose --profile prod up | prod |
Performance Optimization
Resource Management
# docker-compose.yml services: app: deploy: resources: limits: cpus: '0.50' memory: 512M reservations: cpus: '0.25' memory: 256M restart_policy: condition: on-failure delay: 5s max_attempts: 3 window: 120s
Network Configuration
# docker-compose.yml services: app: networks: frontend: ipv4_address: 172.16.238.10 backend: aliases: - api.internal networks: frontend: driver: bridge enable_ipv6: true ipam: driver: default config: - subnet: 172.16.238.0/24 backend: driver: overlay internal: true
Security Implementation
Secrets Management
# docker-compose.yml services: web: secrets: - db_password - ssl_cert environment: - DB_PASSWORD_FILE=/run/secrets/db_password secrets: db_password: file: ./secrets/db_password.txt ssl_cert: file: ./secrets/ssl_cert.pem
Security Options
Option | Purpose | Example |
---|---|---|
cap_drop | Remove capabilities | ALL |
security_opt | Security policies | no-new-privileges |
read_only | Filesystem protection | true |
Monitoring and Logging
Logging Configuration
# docker-compose.yml services: app: logging: driver: "json-file" options: max-size: "10m" max-file: "3" labels: "production" env: "os,customer" labels: com.example.description: "Web application" com.example.environment: "production"
Health Checks
services: web: healthcheck: test: ["CMD", "curl", "-f", "http://localhost:3000/health"] interval: 30s timeout: 10s retries: 3 start_period: 40s
Best Practices
Project Structure
project/ ├── docker-compose.yml # Base configuration ├── docker-compose.override.yml # Development overrides ├── docker-compose.prod.yml # Production settings ├── .env # Environment variables ├── services/ │ ├── web/ │ │ ├── Dockerfile │ │ └── src/ │ └── api/ │ ├── Dockerfile │ └── src/ └── config/ ├── nginx/ └── postgres/
Configuration Management
File | Purpose | Environment |
---|---|---|
compose.yml | Base config | All |
compose.override.yml | Development | Local |
compose.prod.yml | Production | Production |
Troubleshooting Guide
Common Issues
Issue | Cause | Solution |
---|---|---|
Network Conflicts | Port binding | Change ports |
Volume Permissions | User mapping | Set user:group |
Resource Limits | Container limits | Adjust resources |
References
- Docker Compose Documentation
- Compose Specification
- Docker Compose Best Practices
- Networking Guide
- Security Guidelines
- Resource Management
Related Posts
- Docker Desktop Alternatives - Development environment
- Docker Security Scanning - Security tools
- Docker Multi-stage Builds - Build optimization
- Docker Kubernetes Integration - Container orchestration
Docker
Docker Compose
Container Orchestration
DevOps