Containers can silently consume excessive CPU or memory, affecting other containers and the host itself. This guide covers monitoring Docker specifically, beyond general server monitoring tools.
Quick Real-Time Stats
docker stats
Shows live CPU%, memory usage/limit, network I/O, and block I/O for every running container — the fastest way to spot a runaway container.
Stats for a Specific Container
docker stats CONTAINER_NAME
One-Time Snapshot (Non-Streaming)
docker stats --no-stream
Useful for scripting or logging periodic snapshots rather than watching a live stream.
Checking Overall Docker Disk Usage
docker system df
Shows space consumed by images, containers, volumes, and build cache — useful for identifying what's filling up /var/lib/docker.
Detailed Breakdown
docker system df -v
Checking Individual Container Logs Size
sudo du -sh /var/lib/docker/containers/*/*-json.log | sort -hr
See Managing Docker Logs if a specific container's logs are consuming excessive disk space.
Setting Resource Limits to Prevent Runaway Containers
services:
web:
deploy:
resources:
limits:
cpus: '0.5'
memory: 512M
This caps what any single container can consume, protecting the host and other containers.
Monitoring with cAdvisor (Container-Specific Metrics for Prometheus)
docker run -d \
--name cadvisor \
-p 8080:8080 \
-v /:/rootfs:ro \
-v /var/run:/var/run:ro \
-v /sys:/sys:ro \
-v /var/lib/docker/:/var/lib/docker:ro \
gcr.io/cadvisor/cadvisor:latest
cAdvisor exposes detailed per-container metrics that can be scraped by Prometheus — see How to Set Up Prometheus and Grafana for VPS Monitoring for the full monitoring stack integration.
Checking Container Health Status
docker ps
The STATUS column shows (healthy)/(unhealthy) for containers with a configured healthcheck — see Docker Restart Policies and Healthchecks Explained.
Identifying a Runaway Container
docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}" | sort -k2 -hr
Common Errors
A container consumes far more memory than expected — likely a memory leak in the application; set a hard memory limit as a safety net while investigating the root cause.
Host running out of disk from Docker — check docker system df and clean up with docker system prune -a (review what will be removed first).
Best Practices
- Set resource limits on every production container, not just the ones currently causing problems
- Monitor
docker system dfperiodically, not just when disk space becomes critical - Use cAdvisor with Prometheus/Grafana for historical container metrics rather than only real-time snapshots
Related Articles
- Docker Security Best Practices for Production Servers
- Managing Docker Logs
- Docker Restart Policies and Healthchecks Explained
