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
- Extended Logging Configuration
services: app: logging: driver: ${LOG_DRIVER:-json-file} options: max-size: ${LOG_MAX_SIZE:-10m} max-file: ${LOG_MAX_FILE:-3}
- Conditional Logging
services: app: logging: driver: json-file profiles: - production
- 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
- JSON File
services: app: logging: driver: json-file options: max-size: "10m" max-file: "3"
- Syslog
services: app: logging: driver: syslog options: syslog-address: "udp://localhost:514" tag: "app"
- Journald
services: app: logging: driver: journald options: tag: "app"
Docker Swarm Mode Considerations
- Log Management
services: app: logging: driver: json-file options: max-size: "10m" max-file: "3" deploy: placement: constraints: - node.role == manager
- Log Aggregation
services: app: logging: driver: syslog options: syslog-address: "udp://logstash:514" tag: "app"
- 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
- Log Management
- Use appropriate log drivers
- Implement log rotation
- Monitor log size
- Regular log cleanup
- Implement log aggregation
- Log Security
- Protect sensitive information
- Implement access controls
- Regular log analysis
- Log encryption
- Access logging
- Performance Optimization
- Optimize log format
- Use efficient log drivers
- Monitor log performance
- Implement log buffering
- Use log compression
- 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
- 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
- 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
- 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
- Local Logging
- JSON File
- Journald
- Syslog
- Centralized Logging
- ELK Stack
- Graylog
- Fluentd
- Cloud Logging
- AWS CloudWatch
- Azure Monitor
- Google Cloud Logging
- 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.