Back
2/3

Container Monitoring & Metrics

+20 XP on completion

#Container Monitoring & Metrics

After this lesson you'll know:

  • how to monitor container resources with docker stats
  • what cAdvisor is and how to use it
  • how Prometheus & Grafana collect Docker metrics
  • which metrics really matter

#docker stats — live monitoring on the command line

The simplest way to see your container resource usage:

# All running containers
docker stats

# Specific containers
docker stats web db redis

# One-time snapshot (no live view)
docker stats --no-stream

# As JSON for scripting
docker stats --no-stream --format json

docker stats shows: CPU%, Memory (limit/usage), Network I/O, Block I/O, and PIDs.

#cAdvisor — the container monitoring hub

cAdvisor (Container Advisor) by Google automatically collects metrics from all containers on a host and displays them in a web UI:

docker run -d \
  --name=cadvisor \
  --restart=unless-stopped \
  -p 8088:8080 \
  -v /:/rootfs:ro \
  -v /var/run:/var/run:ro \
  -v /sys:/sys:ro \
  -v /var/lib/docker/:/var/lib/docker:ro \
  -v /dev/disk/:/dev/disk:ro \
  gcr.io/cadvisor/cadvisor:latest

# Open http://localhost:8088 — can you see all your containers?

#Prometheus & Grafana — the pro stack

For serious monitoring, combine Prometheus (data collection) with Grafana (dashboards).

# docker-compose.yml for monitoring
services:
  cadvisor:
    image: gcr.io/cadvisor/cadvisor:latest
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:ro
      - /sys:/sys:ro
      - /var/lib/docker/:/var/lib/docker:ro
    ports:
      - "8088:8080"

  prometheus:
    image: prom/prometheus
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    ports:
      - "9090:9090"

  grafana:
    image: grafana/grafana
    ports:
      - "3001:3000"
    environment:
      - GF_AUTH_ANONYMOUS_ENABLED=true
# prometheus.yml
scrape_configs:
  - job_name: 'cadvisor'
    static_configs:
      - targets: ['cadvisor:8080']

#Which metrics matter?

Not all metrics are equally important. Focus on these:

MetricWhy importantWarning threshold
CPU usageContainer eating all cores> 80% sustained
Memory usageContainer can get OOM-killed> 85% of limit
Disk spaceVolumes grow unbounded> 80% of available space
RestartsContainer keeps crashing> 3 restarts in 5 minutes
Healthcheck failuresService not responding3 consecutive failures
# Quick console check
docker stats --no-stream
# -> Do you see a container with critically high memory?

# Restart check
docker ps --filter "status=restarting"

#✋ Try it out

# Try docker stats
docker run -d --name stresser alpine sh -c "while true; do :; done"
docker stats stresser --no-stream
# Can you see CPU at ~100%?
docker rm -f stresser
# Start cAdvisor and explore
docker run -d --name cadvisor -p 8088:8080 --restart=unless-stopped \
  -v /:/rootfs:ro -v /var/run:/var/run:ro -v /sys:/sys:ro \
  -v /var/lib/docker/:/var/lib/docker:ro -v /dev/disk/:/dev/disk:ro \
  gcr.io/cadvisor/cadvisor:latest
# Open http://localhost:8088 and browse through the containers

#📌 Summary

  • docker stats provides quick live metrics on the command line
  • cAdvisor automatically collects all container metrics
  • Prometheus + Grafana are the standard for Docker monitoring
  • Focus on CPU, Memory, Disk, and Restarts — those are the key metrics
← → to navigate