Monitoring containers
What to watch on a Docker host and where the numbers come from: docker stats, the Engine API, events, cAdvisor, Prometheus and Grafana, and alerts.
Inspecting containers: inspect, top, stats and eventsResource limits for memory, CPU and PIDs
Monitoring containers
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Container metrics come from each container's cgroup. `docker stats`, the Engine API and cAdvisor all read the same counters and present them differently.
- `docker stats` CPU % is relative to one CPU, so it can exceed 100% on a multi-core host. Its memory figure subtracts inactive page cache from the cgroup's usage.
- `docker events` streams lifecycle changes such as die, oom and health_status, which are often the earliest signal that something is wrong.
- For history and alerts, run cAdvisor to export per-container metrics to Prometheus and graph them in Grafana. Labels such as the Compose service name come along.
- The metrics that matter most per container: CPU usage against its quota, throttled periods, working-set memory against its limit, OOM events, restarts and health status.
- The daemon's own `metrics-addr` endpoint describes the engine, not your containers' resource usage.
- Alert on symptoms users feel and on clear precursors such as repeated OOM kills or restart loops, not on every brief CPU spike.
What you need to know, and when
A Docker host running a dozen containers raises the same questions every day. Is everything up? Is anything close to its limits? Did something get killed overnight? Is the slowdown one container or the whole host? You need answers now, for a quick look, and over time, for trends and alerts. Docker’s built-in tools cover the first. The second needs a metrics pipeline.
All of it starts at the same place. Each container is a cgroup, and the kernel keeps counters for every cgroup: CPU time, memory, I/O, process count, throttling. The cgroups lesson shows the files. Every tool below reads those counters.
The quick look: docker stats
docker stats --no-stream
docker stats --format 'table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.PIDs}}'
Two numbers in it are often misread, so I read how the CLI computes them in docker/cli’s stats_helpers.go:
- CPU % is the container’s CPU time used between two samples, divided by the host’s total CPU time in the same interval, times the number of online CPUs, times 100. The result is relative to one CPU, so a container using four cores on a 16-core host shows 400%.
- Memory is the cgroup’s usage minus
inactive_fileon cgroup v2 (total_inactive_fileon v1). Inactive file cache is easy for the kernel to reclaim, so subtracting it gives a number closer to what the container needs. The percentage is that over the memory limit, or over host memory if there’s no limit.
The same data is available as JSON from the Engine API, GET /containers/{id}/stats?stream=false, which is what the CLI calls.
The event stream
Numbers tell you about load. Events tell you about change:
docker events --filter type=container \
--filter event=die --filter event=oom --filter event=health_status
A die with a non-zero exit code, an oom, a health_status: unhealthy are each worth knowing about the moment they happen. A restart loop shows up as die and start events repeating. docker events only streams from now, or from a recent window with --since, so for history you need something to collect it.
Over time: cAdvisor, Prometheus, Grafana
The common open-source stack is three pieces:
- cAdvisor, from Google, reads every container’s cgroup and exports Prometheus metrics. Current releases are published as
ghcr.io/google/cadvisor, version 0.61.1 at the time of writing. Its documenteddocker runcommand mounts/,/sys,/var/lib/dockerread-only and/var/runread-write, which includes the Docker socket. That is a trade-off to make knowingly, given what the socket is. - Prometheus scrapes cAdvisor every few seconds and stores the series, as DevOps’ metrics with Prometheus explains.
- Grafana graphs them. See dashboards with Grafana.
A minimal Compose file for the three, run on the host it watches:
services:
cadvisor:
image: ghcr.io/google/cadvisor:v0.61.1
volumes:
- /:/rootfs:ro
- /var/run:/var/run:rw
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
restart: unless-stopped
prometheus:
image: prom/prometheus
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
restart: unless-stopped
grafana:
image: grafana/grafana
ports:
- "127.0.0.1:3000:3000"
restart: unless-stopped
# prometheus.yml
scrape_configs:
- job_name: cadvisor
static_configs:
- targets: ["cadvisor:8080"]
cAdvisor attaches the container’s labels to its metrics, so Compose’s com.docker.compose.service label lets you group by service rather than by container ID.
The metrics that matter
From cAdvisor’s metric reference, the ones I would put on the first dashboard:
| Question | Metric |
|---|---|
| How much CPU? | rate(container_cpu_usage_seconds_total[5m]) |
| Is it throttled? | rate(container_cpu_cfs_throttled_periods_total[5m]) / rate(container_cpu_cfs_periods_total[5m]) |
| How much memory? | container_memory_working_set_bytes against container_spec_memory_limit_bytes |
| Was it OOM-killed? | container_oom_events_total |
| Network | container_network_receive_bytes_total and its transmit twin |
| Is it still there? | container_last_seen |
Throttling is the one people miss. As the resource limits lesson showed, a container can be throttled while its average CPU looks modest, and only the throttle ratio reveals it.
Add host-level metrics too, usually from Prometheus’ node exporter: disk space under /var/lib/docker fills with images, volumes and unrotated logs long before CPU is a problem.
The daemon has its own metrics endpoint, enabled with "metrics-addr": "127.0.0.1:9323" in daemon.json. It describes the engine itself, such as its own resource use and operation counts, not your containers’ cgroups.
Logs and alerts
Metrics say that something is wrong. Logs usually say why. Ship container logs off the host with a logging driver or a collector, as in container logs and logging drivers and DevOps’ log aggregation pipelines.
For alerts, follow DevOps’ advice to alert on symptoms, not causes: page on what users feel, such as errors and latency from the application, and use container metrics for strong precursors. Good container alerts: OOM events above zero, a container restarting several times in ten minutes, a health check unhealthy for more than a few minutes, sustained throttling on a latency-sensitive service, the Docker disk above 85%. A brief CPU spike isn’t one.
About the video
The embed is all of “Monitor Docker Containers Like a Pro with Grafana & Prometheus” from Ehsan’s Tech Cave, November 2024, 11 minutes. The chapters go from why monitoring is needed, through deploying cAdvisor, then Prometheus and Grafana, initial setup, building the dashboard, adding more Docker hosts, and using custom labels. That is the stack in this lesson, built on screen. It doesn’t explain how docker stats calculates its numbers or which metrics to alert on, which the lesson adds, and it may use the older gcr.io cAdvisor image path. YouTube reports no audio language, so English was judged from the title, description and English captions. Sematext’s video in deeper is a vendor’s overview of which Docker metrics matter.
What was run: the CPU and memory calculations were read from docker/cli’s source, and cAdvisor’s metric names and run command from its documentation. The Compose file was loaded with compose-go to check it parses. No stack was started, since no daemon was available. Everything was checked 2026-11-28.
Where this fits
You can run and watch containers on a host. Most images, though, are built and tested by machines, not people: Docker in CI pipelines.
Check yourself
docker stats shows a container at 250% CPU. Is that a bug?
No. The CPU percentage is calculated relative to one CPU and multiplied by the number of online CPUs, so a container using two and a half cores shows 250%.
A container's memory in docker stats looks lower than memory.current in its cgroup. Why?
The CLI subtracts inactive_file, page cache the kernel can reclaim easily, from the usage. It's closer to the memory the container really needs.
Which cAdvisor metric pair tells you a container is being CPU-throttled, and how do you turn it into a ratio?
container_cpu_cfs_throttled_periods_total divided by container_cpu_cfs_periods_total, each as a rate over the same window. A high ratio means it often hits its quota.
Which command watches, live, for containers being killed for exceeding their memory limit?
docker events --filter event=oom. It prints an event each time the kernel OOM-kills a process in a container.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.