How to monitor Docker containers in production?
· Category: Docker
Short answer
Monitor Docker containers using built-in commands like docker stats, combined with tools like cAdvisor for metrics and Prometheus for collection. Centralize logs with Fluentd or the Docker logging driver.
Steps
- Enable metrics with
docker statsfor quick checks. - Deploy cAdvisor to expose container metrics.
- Scrape metrics with Prometheus.
- Visualize with Grafana dashboards.
- Forward logs to a centralized system.
Example
docker run -d --name cadvisor --volume=/:/rootfs:ro --volume=/var/run:/var/run:ro --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro --publish=8080:8080 gcr.io/cadvisor/cadvisor:latest
Prometheus scrape config:
scrape_configs:
- job_name: 'cadvisor'
static_configs:
- targets: ['cadvisor:8080']
Tips
- Set up alerts for CPU, memory, and restart thresholds.
- Use structured logging for easier parsing and querying.
- Monitor the Docker daemon itself, not just containers.
Common issues
- High-cardinality metrics can overwhelm Prometheus.
- cAdvisor can consume significant CPU on hosts with many containers.
- Log rotation must be configured to prevent disk exhaustion.