---
title: "Real-Time Container Resource Monitoring with `docker stats`"
date: "2026-11-17T12:00:00+07:00"
description: "Master live performance metrics in Docker using docker stats, formatting output with Go templates, snapshot monitoring with --no-stream, and tracking CPU, memory, and I/O usage."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/real-time-resource-monitoring-docker-stats.jpg"
alt: "Terminal displaying docker stats real-time metric table with CPU, RAM, and Network I/O"
tags: ['docker', 'containers', 'monitoring', 'sysadmin', 'devops']
---

Monitoring resource consumption is a core operational requirement when running containerized applications in staging or production. Without visibility into container metrics, application memory leaks, CPU spikes, or runaway disk I/O operations can degrade host performance and crash adjacent services.

The Docker CLI includes a lightweight built-in tool: `docker stats`. This command streams real-time CPU, memory, network, and block I/O statistics directly from Linux cgroups without installing third-party monitoring agents.

---

## Basic Usage of `docker stats`

Executing `docker stats` without arguments streams live resource utilization metrics for all currently running containers:

```bash
docker stats
```

The terminal updates continuously (every second), producing a dynamic tabular view:

```text
CONTAINER ID   NAME         CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O         PIDS
a1b2c3d4e5f6   web-api      0.45%     45.2MiB / 1.952GiB   2.26%     1.2MB / 450kB     12.4MB / 0B       14
b7c8d9e0f1a2   redis-cache  0.02%     8.12MiB / 1.952GiB   0.41%     240kB / 180kB     0B / 0B           5
c3d4e5f6a7b8   db-postgres  2.10%     128.4MiB / 1.952GiB  6.42%     5.4MB / 12.1MB    45.8MB / 102MB    28
```

To limit monitoring to specific containers, pass container names or IDs as arguments:

```bash
docker stats web-api db-postgres
```

---

## Understanding the Reported Metrics

| Column Metric | Description & Technical Context |
| :--- | :--- |
| **`CONTAINER ID / NAME`** | The short identifier and user-assigned name of the container. |
| **`CPU %`** | Percentage of host CPU capacity used. On multi-core systems, this percentage can exceed 100% (e.g., 200% = 2 full CPU cores utilized). |
| **`MEM USAGE / LIMIT`** | Current RAM consumed versus maximum allocated RAM limit (or total host RAM if no limit is set). |
| **`MEM %`** | Percentage of container memory limit currently in use. |
| **`NET I/O`** | Total volume of data received (`RX`) and transmitted (`TX`) over virtual network interfaces. |
| **`BLOCK I/O`** | Total volume of read and write operations performed on host storage drives. |
| **`PIDS`** | Number of active processes or kernel threads created inside the container's control group (cgroup). |

---

## One-Shot Snapshot Monitoring (`--no-stream`)

By default, `docker stats` runs indefinitely, streaming live metrics until interrupted with `Ctrl+C`. 

When writing shell scripts, health checks, or automated diagnostic routines, pass the `--no-stream` flag to print a single static snapshot of current metrics and exit immediately:

```bash
docker stats --no-stream
```

Example output in automated logging scripts:

```bash
# Capture metrics snapshot to log file
docker stats --no-stream > /var/log/container-metrics-$(date +%Y%m%d-%H%M).log
```

---

## Formatting Output with Go Templates (`--format`)

The real power of `docker stats` lies in custom output formatting using Go template syntax. The `--format` flag allows system administrators to slice metrics, extract specific columns, or produce JSON output for automated parsing.

### 1. Creating Custom Plain Text Tables

Display only container names, CPU percentage, and memory usage:

```bash
docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"
```

Output:

```text
NAME         CPU %     MEM USAGE / LIMIT
web-api      0.45%     45.2MiB / 1.952GiB
redis-cache  0.02%     8.12MiB / 1.952GiB
db-postgres  2.10%     128.4MiB / 1.952GiB
```

### 2. Outputting Structured JSON

To pipe metric data into command-line utilities like `jq` or send metrics to logging platforms, format output as JSON:

```bash
docker stats --no-stream --format '{{json .}}'
```

Output (formatted JSON per line):

```json
{"BlockIO":"12.4MB / 0B","CPUPerc":"0.45%","Container":"a1b2c3d4e5f6","ID":"a1b2c3d4e5f6","MemPerc":"2.26%","MemUsage":"45.2MiB / 1.952GiB","Name":"web-api","NetIO":"1.2MB / 450kB","PIDs":"14"}
{"BlockIO":"0B / 0B","CPUPerc":"0.02%","Container":"b7c8d9e0f1a2","ID":"b7c8d9e0f1a2","MemPerc":"0.41%","MemUsage":"8.12MiB / 1.952GiB","Name":"redis-cache","NetIO":"240kB / 180kB","PIDs":"5"}
```

### 3. Extracting High CPU Usage Containers with `jq`

Parse JSON output to flag containers consuming more memory or CPU than expected:

```bash
docker stats --no-stream --format '{{json .}}' | jq 'select(.CPUPerc > "50.00%")'
```

---

## Configuring Default CLI Stats Format

If you frequently run `docker stats` with custom column layouts, set a global default template in your local Docker configuration file (`~/.docker/config.json`):

```json
{
  "statsFormat": "table {{.Name}}\t{{.CPUPerc}}\t{{.MemPerc}}\t{{.NetIO}}\t{{.PIDs}}"
}
```

Now running plain `docker stats` automatically applies your preferred formatting layout without needing the `--format` flag.

---

## Monitoring Resource Limits Enforcement

`docker stats` is particularly useful for verifying resource constraints imposed by Docker runtime flags (`--memory`, `--cpus`):

```bash
# Launch container with 512MB RAM limit and 1.5 CPUs
docker run -d --name memory-constrained-app \
  --memory="512m" \
  --cpus="1.5" \
  nginx:alpine
```

Check `docker stats`:

```bash
docker stats memory-constrained-app --no-stream
```

Output verifies that `MEM USAGE / LIMIT` displays the enforced limit (`512MiB`) rather than total host RAM.

---

## **Takeaway:**

The `docker stats` command provides lightweight, real-time insight into container CPU, RAM, disk I/O, network traffic, and PID counts directly from cgroups. Use `--no-stream` for single-shot script snapshots, and leverage `--format '{{json .}}'` to parse metric data into structured logging pipelines.

---

## Sources

- [Docker Docs: docker stats](https://docs.docker.com/reference/cli/docker/container/stats/)
- [Docker Docs: Format command and log output](https://docs.docker.com/config/formatting/)
- [Docker Docs: Limit a container's resources](https://docs.docker.com/config/containers/resource_constraints/)
