Docker Compose V2: The Complete Guide to Modern Container Orchestration
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

FeatureDescriptionBenefits
Compose SpecStandardized formatPortability
ProfilesEnvironment groupingFlexibility
IncludeComposition reuseModularity

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

EnvironmentCommandProfile
Developmentcompose updev
Testingcompose --profile test uptest
Productioncompose --profile prod upprod

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

OptionPurposeExample
cap_dropRemove capabilitiesALL
security_optSecurity policiesno-new-privileges
read_onlyFilesystem protectiontrue

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

FilePurposeEnvironment
compose.ymlBase configAll
compose.override.ymlDevelopmentLocal
compose.prod.ymlProductionProduction

Troubleshooting Guide

Common Issues

IssueCauseSolution
Network ConflictsPort bindingChange ports
Volume PermissionsUser mappingSet user:group
Resource LimitsContainer limitsAdjust resources

References

  1. Docker Compose Documentation
  2. Compose Specification
  3. Docker Compose Best Practices
  4. Networking Guide
  5. Security Guidelines
  6. Resource Management

Related Posts

Docker
Docker Compose
Container Orchestration
DevOps