Stats Streaming

Difficulty: Beginner

Question

How do you get a live view of CPU, memory, network and disk I/O for all running containers, and how do you read the numbers correctly?

Answer

Run docker stats. It opens a live, auto-refreshing table with one row per running container showing CPU %, memory usage against its limit, memory %, network I/O, block I/O and the number of processes (PIDS). Add --no-stream for a single snapshot, -a to include stopped containers, and --format to pick columns for scripts.

Where the numbers come from

Docker does not sample processes itself. It reads the kernel's cgroup accounting for each container (CPU time, memory, I/O and PIDs) through the Engine API endpoint /containers/{id}/stats, and computes rates between two readings. That is why the first refresh can briefly show zeros, and why the figures match what the kernel enforces for limits.

Reading each column without fooling yourself

ColumnWhat it meansCommon misread
CPU %Share of host CPU time used since the last sampleCan exceed 100% on multi-core hosts; 250% means about 2.5 cores busy
MEM USAGE / LIMITMemory charged to the container's cgroup against its limitIf no --memory is set, LIMIT is the host's total RAM
MEM %Usage divided by limitNear 100% with a limit set means an OOM kill is close
NET I/OBytes received and sent since container startCumulative, not per second
BLOCK I/OBytes read and written to block devicesCumulative; often 0B for files served from page cache
PIDSProcesses and threads in the containerSteady growth suggests a thread or process leak

On Linux, the memory figure subtracts reclaimable file cache (for example inactive_file on cgroup v2), so it is closer to what the OOM killer cares about than raw cgroup usage. It will still not match a JVM's heap number or Node's process.memoryUsage() exactly, because those measure from inside the runtime.

Using it in practice

  • Quick triage: a Swiggy-style order service suddenly slow? docker stats shows in seconds whether one container is pinned at its CPU quota or creeping toward its memory limit.
  • Load testing: run k6 or JMeter against a service and watch the container's CPU and memory on a second terminal to find the saturation point before setting resource limits.
  • Scripts and CI: docker stats --no-stream --format "{{.Name}},{{.CPUPerc}},{{.MemUsage}}" gives one CSV line per container that you can log or assert on.

Limits of docker stats

It has no history, no alerting and covers one host only. For production you would scrape the same cgroup metrics with cAdvisor or the node exporter into Prometheus and graph them in Grafana, or rely on the cloud provider's container insights. In Kubernetes the equivalent quick look is kubectl top pods, backed by metrics-server. Treat docker stats as the top command for containers: perfect for a live look, not a monitoring system.

Where docker stats numbers come from. The kernel accounts usage per cgroup and the CLI turns two readings into rates.
  1. Container processes: Use CPU, memory, disk and network.
  2. Kernel cgroup counters: The kernel accounts usage per container cgroup.
  3. Docker Engine stats API: Reads counters and streams them as JSON.
  4. docker stats table: CLI computes percentages and refreshes the view.

Code examples

Live view, snapshot and script-friendly output

# Live, refreshing view of all running containers
docker stats

# Only specific containers
docker stats api worker

# One snapshot, custom columns, good for logs or CI
docker stats --no-stream \
  --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}\t{{.PIDs}}"

The api container is using more than one core and is at 80% of a 512 MiB limit, so it is the one to investigate. worker and redis show the host's RAM as their limit, meaning no memory limit was set.

Key points

  • docker stats streams live by default; --no-stream prints one snapshot
  • CPU % is relative to one core, so it can exceed 100% on multi-core machines
  • If no memory limit is set, the LIMIT column shows total host memory
  • NET I/O and BLOCK I/O are cumulative since start, not rates
  • For history and alerts use cAdvisor or node exporter with Prometheus and Grafana

Common follow-up questions

  • Why might the memory shown in docker stats differ from what the application reports?
  • How would you set CPU and memory limits after seeing these numbers?
  • What happens when a container hits its memory limit?
  • How do you monitor containers across 50 hosts?

Interview tips

  • Explain that the data comes from cgroups; it shows you know how containers are isolated
  • Point out the CPU over 100% and the 'limit equals host RAM' cases; interviewers like those details
  • Close by saying docker stats is for live triage and naming a real monitoring stack for production

Concepts covered

docker stats, cgroups, Memory usage vs limit, CPU percentage, --no-stream, --format

More Docker & Containers questions