Complete Guide to Docker Compose Logging

Effective log management is crucial for monitoring and troubleshooting Docker Compose applications. This guide covers various logging strategies and configurations.

Basic Logging Configuration

services:
  app:
    image: myapp:latest
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"
        compress: "true"
    environment:
      - LOG_LEVEL=info
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
      interval: 30s
      timeout: 10s
      retries: 3

Docker Compose 3.x Features

  1. Extended Logging Configuration
    services:
      app:
        logging:
          driver: ${LOG_DRIVER:-json-file}
          options:
            max-size: ${LOG_MAX_SIZE:-10m}
            max-file: ${LOG_MAX_FILE:-3}
    
  2. Conditional Logging
    services:
      app:
        logging:
          driver: json-file
        profiles:
          - production
    
  3. Logging Templates
    services:
      app:
        logging:
          driver: ${LOG_DRIVER_${SERVICE_NAME:-app}:-json-file}
          options:
            max-size: ${LOG_MAX_SIZE_${SERVICE_NAME:-app}:-10m}
    

Log Drivers

  1. JSON File
    services:
      app:
        logging:
          driver: json-file
          options:
            max-size: "10m"
            max-file: "3"
    
  2. Syslog
    services:
      app:
        logging:
          driver: syslog
          options:
            syslog-address: "udp://localhost:514"
            tag: "app"
    
  3. Journald
    services:
      app:
        logging:
          driver: journald
          options:
            tag: "app"
    

Docker Swarm Mode Considerations

  1. Log Management
    services:
      app:
        logging:
          driver: json-file
          options:
            max-size: "10m"
            max-file: "3"
        deploy:
          placement:
            constraints:
              - node.role == manager
    
  2. Log Aggregation
    services:
      app:
        logging:
          driver: syslog
          options:
            syslog-address: "udp://logstash:514"
            tag: "app"
    
  3. Log Rotation
    services:
      app:
        logging:
          driver: json-file
          options:
            max-size: "10m"
            max-file: "3"
            compress: "true"
    

Common Logging Patterns

1. Application Logs

services:
  app:
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"
    environment:
      - LOG_LEVEL=info
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
      interval: 30s
      timeout: 10s
      retries: 3

2. Access Logs

services:
  nginx:
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"
        compress: "true"
    environment:
      - ACCESS_LOG=/var/log/nginx/access.log

3. Error Logs

services:
  app:
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"
        compress: "true"
    environment:
      - ERROR_LOG=/var/log/app/error.log

Best Practices

  1. Log Management
    • Use appropriate log drivers
    • Implement log rotation
    • Monitor log size
    • Regular log cleanup
    • Implement log aggregation
  2. Log Security
    • Protect sensitive information
    • Implement access controls
    • Regular log analysis
    • Log encryption
    • Access logging
  3. Performance Optimization
    • Optimize log format
    • Use efficient log drivers
    • Monitor log performance
    • Implement log buffering
    • Use log compression
  4. Maintenance
    • Regular log inspection
    • Cleanup procedures
    • Log analysis
    • Usage monitoring
    • Documentation

Log Operations

1. Log Management

# View logs
docker compose logs app

# Follow logs
docker compose logs -f app

# View last N lines
docker compose logs --tail=100 app

# View logs since timestamp
docker compose logs --since="2025-04-21T00:00:00" app

2. Log Analysis

# Search logs
docker compose logs app | grep "error"

# Count log entries
docker compose logs app | wc -l

# Analyze log patterns
docker compose logs app | awk '{print $4}' | sort | uniq -c

3. Log Monitoring

# Monitor log size
du -sh /var/lib/docker/containers/*/*-json.log

# Check log rotation
ls -l /var/lib/docker/containers/*/*-json.log*

# Monitor log growth
watch -n 1 'du -sh /var/lib/docker/containers/*/*-json.log'

Advanced Logging Features

1. Log Drivers

services:
  app:
    logging:
      driver: syslog
      options:
        syslog-address: "udp://localhost:514"
        tag: "app"
        syslog-facility: "daemon"

2. Log Labels

services:
  app:
    logging:
      driver: json-file
      options:
        labels: "production"
        env: "prod"

3. Log Templates

services:
  app:
    logging:
      driver: ${LOG_DRIVER_${SERVICE_NAME:-app}:-json-file}
      options:
        max-size: ${LOG_MAX_SIZE_${SERVICE_NAME:-app}:-10m}
    environment:
      - LOG_DRIVER_app=json-file

Troubleshooting

  1. Log Issues
    # Check log configuration
    docker inspect app
    
    # View log driver
    docker info | grep Logging
    
    # Check log files
    ls -l /var/lib/docker/containers/*/*-json.log
    
  2. Performance Problems
    # Monitor log growth
    watch -n 1 'du -sh /var/lib/docker/containers/*/*-json.log'
    
    # Check disk usage
    df -h
    
    # Analyze I/O
    iostat -x 1
    
  3. Log Analysis
    # Search for errors
    docker compose logs app | grep -i error
    
    # Count log entries
    docker compose logs app | wc -l
    
    # Analyze patterns
    docker compose logs app | awk '{print $4}' | sort | uniq -c
    

Integration with Logging Solutions

  1. Local Logging
    • JSON File
    • Journald
    • Syslog
  2. Centralized Logging
    • ELK Stack
    • Graylog
    • Fluentd
  3. Cloud Logging
    • AWS CloudWatch
    • Azure Monitor
    • Google Cloud Logging
  4. Log Analysis
    • Prometheus
    • Grafana
    • Loki

Remember to regularly review and update your logging strategies based on the latest best practices and requirements. Implement a comprehensive logging plan and continuously monitor your Docker Compose setup for optimal 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.