cAdvisor (Container Advisor) provides container-level resource usage and performance metrics. Developed by Google, it auto-discovers running containers and collects CPU, memory, network, and filesystem metrics — essential data for monitoring Docker and Kubernetes environments. This guide covers deploying cAdvisor and integrating with Prometheus and Grafana.
Deployment
# Docker (recommended)
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 \
--volume=/dev/disk/:/dev/disk:ro \
--privileged \
--device=/dev/kmsg \
-p 8080:8080 \
gcr.io/cadvisor/cadvisor:v0.49.1
# Access web UI at http://localhost:8080
# Prometheus metrics at http://localhost:8080/metrics
Docker Compose
services:
cadvisor:
image: gcr.io/cadvisor/cadvisor:v0.49.1
container_name: cadvisor
privileged: true
devices:
- /dev/kmsg:/dev/kmsg
volumes:
- /:/rootfs:ro
- /var/run:/var/run:ro
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
- /dev/disk/:/dev/disk:ro
ports:
- "8080:8080"
restart: unless-stopped
Prometheus Integration
# prometheus.yml
scrape_configs:
- job_name: cadvisor
static_configs:
- targets: ['cadvisor:8080']
scrape_interval: 15s
metrics_path: /metrics
Key Metrics
# CPU usage per container (percentage of one core)
rate(container_cpu_usage_seconds_total{name!=""}[5m]) * 100
# Memory usage per container
container_memory_usage_bytes{name!=""}
# Memory limit vs usage
container_memory_usage_bytes{name!=""} / container_spec_memory_limit_bytes{name!=""} * 100
# Network I/O per container
rate(container_network_receive_bytes_total{name!=""}[5m])
rate(container_network_transmit_bytes_total{name!=""}[5m])
# Disk I/O per container
rate(container_fs_reads_bytes_total{name!=""}[5m])
rate(container_fs_writes_bytes_total{name!=""}[5m])
# Container count
count(container_last_seen{name!=""})
# Container restart count
changes(container_start_time_seconds{name!=""}[1h])
Grafana Dashboard
# Import dashboard ID: 14282 (Docker Container & Host Metrics)
# Or ID: 893 (Docker monitoring with cAdvisor)
Alert Rules
groups:
- name: container_alerts
rules:
- alert: ContainerHighCPU
expr: rate(container_cpu_usage_seconds_total{name!=""}[5m]) > 0.9
for: 5m
labels:
severity: warning
annotations:
summary: "Container {{ $labels.name }} CPU > 90%"
- alert: ContainerHighMemory
expr: container_memory_usage_bytes{name!=""} / container_spec_memory_limit_bytes{name!=""} > 0.9
for: 5m
labels:
severity: warning
- alert: ContainerRestarting
expr: changes(container_start_time_seconds{name!=""}[15m]) > 2
labels:
severity: critical
annotations:
summary: "Container {{ $labels.name }} restarted {{ $value }} times in 15 minutes"
Best Practices
- Run cAdvisor as a privileged container for full metric collection
- Set a 15-30 second scrape interval — container metrics change rapidly
- Filter out system containers (pause, POD) in Kubernetes environments
- Monitor container memory relative to limits, not absolute usage
- Alert on container restart frequency — frequent restarts indicate application issues
- Use cAdvisor alongside node_exporter for complete infrastructure visibility