Docker Compose Performance Optimization

Performance optimization is crucial for efficient containerized applications. This guide covers various strategies to improve Docker Compose performance.

Basic Performance Configuration

services:
  app:
    image: myapp:latest
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 512M
        reservations:
          cpus: '0.25'
          memory: 256M
    tmpfs:
      - /tmp:rw,noexec,nosuid
    ulimits:
      nofile:
        soft: 65535
        hard: 65535
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
      interval: 30s
      timeout: 10s
      retries: 3
    restart: unless-stopped

Docker Compose 3.x Features

  1. Extended Resource Management
    services:
      app:
        deploy:
          resources:
            limits:
              cpus: ${CPU_LIMIT:-'0.50'}
              memory: ${MEMORY_LIMIT:-512M}
            reservations:
              cpus: ${CPU_RESERVATION:-'0.25'}
              memory: ${MEMORY_RESERVATION:-256M}
    
  2. Conditional Optimization
    services:
      app:
        deploy:
          resources:
            limits:
              cpus: ${OPTIMIZE_CPU:-'0.50'}
              memory: ${OPTIMIZE_MEMORY:-512M}
        profiles:
          - optimized
    
  3. Performance Templates
    services:
      app:
        deploy:
          resources:
            limits:
              cpus: ${CPU_${SERVICE_NAME:-app}_LIMIT:-'0.50'}
              memory: ${MEMORY_${SERVICE_NAME:-app}_LIMIT:-512M}
    

Performance Optimization Areas

  1. Resource Management
    services:
      app:
        deploy:
          resources:
            limits:
              cpus: '0.50'
              memory: 512M
            reservations:
              cpus: '0.25'
              memory: 256M
          restart_policy:
            condition: on-failure
            max_attempts: 3
    
  2. Caching Strategies
    services:
      app:
        volumes:
          - app_cache:/app/.cache:delegated
          - node_modules:/app/node_modules:delegated
        build:
          cache_from:
            - myapp:latest
            - myapp:${BUILD_CACHE_TAG:-latest}
    
  3. Container Tuning
    services:
      app:
        ulimits:
          nofile:
            soft: 65535
            hard: 65535
          nproc: 65535
        tmpfs:
          - /tmp:rw,noexec,nosuid,size=100m
        shm_size: 64m
    

Docker Swarm Mode Considerations

  1. Service Scaling
    services:
      app:
        deploy:
          mode: replicated
          replicas: ${REPLICAS:-3}
          update_config:
            parallelism: 2
            delay: 10s
            failure_action: rollback
          restart_policy:
            condition: on-failure
    
  2. Resource Distribution
    services:
      app:
        deploy:
          placement:
            constraints:
              - node.role == manager
          resources:
            limits:
              cpus: '0.50'
              memory: 512M
    
  3. Load Balancing
    services:
      app:
        deploy:
          endpoint_mode: dnsrr
          update_config:
            order: start-first
    

Common Optimization Patterns

1. Resource Allocation

services:
  app:
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 512M
        reservations:
          cpus: '0.25'
          memory: 256M
      restart_policy:
        condition: on-failure
        max_attempts: 3

2. Volume Optimization

services:
  app:
    volumes:
      - app_data:/data:delegated
      - ./src:/app:cached
      - node_modules:/app/node_modules:delegated
    tmpfs:
      - /tmp:rw,noexec,nosuid,size=100m

3. Network Optimization

services:
  app:
    networks:
      - app_network
    dns:
      - 8.8.8.8
      - 8.8.4.4
    dns_search:
      - example.com
    extra_hosts:
      - "hostname:172.17.0.1"

networks:
  app_network:
    driver: bridge
    driver_opts:
      com.docker.network.bridge.enable_icc: "true"
      com.docker.network.bridge.enable_ip_masquerade: "true"

Best Practices

  1. Resource Management
    • Set appropriate limits
    • Monitor resource usage
    • Implement auto-scaling
    • Use resource reservations
    • Implement health checks
  2. Storage Optimization
    • Use appropriate volume drivers
    • Implement caching
    • Optimize I/O operations
    • Use tmpfs for temporary data
    • Implement volume delegation
  3. Network Performance
    • Use appropriate network drivers
    • Optimize DNS resolution
    • Implement connection pooling
    • Use network aliases
    • Optimize network settings
  4. Container Optimization
    • Use appropriate base images
    • Implement multi-stage builds
    • Optimize layer caching
    • Use .dockerignore
    • Implement container health checks

Performance Monitoring

1. Resource Usage

# Monitor container resources
docker stats

# Check resource limits
docker compose config

# Monitor system resources
docker system df -v

2. Performance Metrics

# Check container performance
docker compose top

# Monitor container processes
docker compose exec app top

# Check container stats
docker compose ps --format json

3. Network Performance

# Test network connectivity
docker compose exec app ping -c 4 google.com

# Check network latency
docker compose exec app curl -o /dev/null -s -w '%{time_total}\n' http://example.com

# Monitor network traffic
docker compose exec app netstat -tulpn

Advanced Optimization Features

1. Container Tuning

services:
  app:
    ulimits:
      nofile:
        soft: 65535
        hard: 65535
      nproc: 65535
    tmpfs:
      - /tmp:rw,noexec,nosuid,size=100m
    shm_size: 64m
    cap_add:
      - NET_ADMIN

2. Build Optimization

services:
  app:
    build:
      context: .
      cache_from:
        - myapp:latest
        - myapp:${BUILD_CACHE_TAG:-latest}
      args:
        - NODE_ENV=production
        - BUILD_OPTIMIZE=true
      target: production

3. Service Scaling

services:
  app:
    deploy:
      replicas: ${REPLICAS:-3}
      update_config:
        parallelism: 2
        delay: 10s
        failure_action: rollback
      restart_policy:
        condition: on-failure
        max_attempts: 3

Troubleshooting

  1. Performance Issues
    # Check container stats
    docker stats
    
    # Monitor system resources
    docker system df -v
    
    # Check container logs
    docker compose logs --tail=100
    
  2. Resource Bottlenecks
    # Monitor system resources
    docker system df
    
    # Check container processes
    docker compose top
    
    # Analyze resource usage
    docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"
    
  3. Network Problems
    # Test network performance
    docker compose exec app curl -o /dev/null -s -w '%{time_total}\n' http://example.com
    
    # Check network connectivity
    docker compose exec app ping -c 4 google.com
    
    # Network diagnostics
    docker compose exec app netstat -tulpn
    

Integration with Monitoring Tools

  1. Resource Monitoring
    • Prometheus
    • Grafana
    • cAdvisor
    • Node Exporter
    • Blackbox Exporter
  2. Application Performance
    • New Relic
    • Datadog
    • AppDynamics
    • Dynatrace
    • Elastic APM
  3. Log Analysis
    • ELK Stack
    • Fluentd
    • Graylog
    • Loki
    • Promtail
  4. Container Orchestration
    • Docker Swarm
    • Kubernetes
    • Nomad
    • Mesos
    • OpenShift

Remember to regularly monitor and adjust your performance configurations based on actual usage patterns and requirements. Implement a comprehensive monitoring strategy and continuously optimize your Docker Compose setup for better performance.

© 2025 Compose-it. All rights reserved.

Docker are registered trademarks of Docker, Inc. in the United States and/or other countries. The tool 'compose-it' is not affiliated with, endorsed by, or sponsored by Docker, Inc.