# Container metrics with cAdvisor and docker stats

> docker stats shows live per-container CPU and memory in the terminal, while cAdvisor exports the same data as Prometheus metrics so you can graph and alert on each container.

URL: https://levelrail.com/learn/monitoring/container-metrics-with-cadvisor-and-docker-stats/
Updated: 2026-11-27

`docker stats` gives you a live table of CPU and memory per container, with `--no-stream` and `--format` for scripts. cAdvisor runs as a container, reads usage from the host and serves it as Prometheus metrics, so you can graph `container_memory_working_set_bytes` and `rate(container_cpu_usage_seconds_total[5m])` per container. Keep its port on localhost or a private network.

## How it works

Linux control groups (cgroups) record the CPU, memory, network and disk used by each container. `docker stats` asks the Docker daemon for those numbers and prints them. cAdvisor reads the same host data directly and exposes it at `/metrics`, with a label per container, so Prometheus can scrape and store it over time. Host-level metrics from node_exporter, covered in [Prometheus and node exporter basics](https://levelrail.com/learn/monitoring/prometheus-and-node-exporter-basics/)), tell you the server is low on memory. cAdvisor tells you which container is responsible. See the glossary entry for [container](https://levelrail.com/learn/glossary/#container)) if the term is new.

## Step 1: Use docker stats

```bash
docker stats
```

The table refreshes until you press Ctrl+C. Columns include CPU percent, memory usage against its limit, network and block I/O and the number of processes. For a single reading:

```bash
docker stats --no-stream
docker stats --no-stream --format "{{.Name}} {{.CPUPerc}} {{.MemUsage}}"
```

`--no-stream` prints once and exits, so cron and scripts can use it. `--format` takes a Go template. If a container has no memory limit, the limit column shows the host's total memory, so a container can use much more than you intended. Limits are set with `--memory` or in Compose, as shown in [docker compose basics](https://levelrail.com/learn/docker/docker-compose-basics/)).

## Step 2: Run cAdvisor

cAdvisor needs read-only access to host information. This Compose service follows the example in the cAdvisor project README:

```yaml
services:
  cadvisor:
    image: gcr.io/cadvisor/cadvisor:v0.49.1
    container_name: cadvisor
    restart: unless-stopped
    ports:
      - "127.0.0.1:8080:8080"
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:ro
      - /sys:/sys:ro
      - /var/lib/docker/:/var/lib/docker:ro
    privileged: true
    devices:
      - /dev/kmsg
```

The mounts give it read access to the host root, runtime sockets, `/sys` for cgroup data and Docker's data directory. Check the README for your version, since the exact list can change, and pin the tag you tested instead of `latest`. The `privileged` setting is broad, so only run cAdvisor on hosts you control and keep the port off the public network. A restart policy is explained in [Docker restart policies](https://levelrail.com/learn/docker/docker-restart-policies/)).

The port binds to `127.0.0.1:8080`. cAdvisor has a web UI and a metrics page that list every container, image name and mount with no login. Do not publish it as `8080:8080`, which listens on all interfaces. See [publish container ports safely](https://levelrail.com/learn/docker/publish-container-ports-safely/)).

## Step 3: Scrape it from Prometheus

Add a job to `prometheus.yml`:

```yaml
  - job_name: cadvisor
    static_configs:
      - targets: ["127.0.0.1:8080"]
```

If Prometheus runs in Docker, put both on a shared Docker network and use `cadvisor:8080` as the target instead. Validate and restart:

```bash
promtool check config /etc/prometheus/prometheus.yml
sudo systemctl restart prometheus
```

Look for the `cadvisor` job on the targets page, and confirm it is `UP`.

## Step 4: Query container metrics

```text
rate(container_cpu_usage_seconds_total{name!=""}[5m])
container_memory_working_set_bytes{name!=""}
```

The first gives CPU cores used per container. A value of 0.5 means half a core. The second is memory the container actively uses, which is the figure closest to what triggers an out-of-memory kill and is better than raw usage that includes cache. The filter `name!=""` drops cgroup entries that are not named containers. Graph these in Grafana as in [Grafana dashboards for CPU, memory and disk](https://levelrail.com/learn/monitoring/grafana-dashboards-for-cpu-memory-disk/)), and compare against the limit to alert before a container hits it.

## Check it worked

- `docker stats --no-stream` lists each running container with believable numbers.
- `docker ps --filter name=cadvisor` shows the port bound to `127.0.0.1`.
- `curl -s http://127.0.0.1:8080/metrics | grep container_memory_working_set_bytes` prints lines.
- The `cadvisor` target is `UP` in Prometheus.
- From another machine, `curl --max-time 5 http://203.0.113.10:8080/metrics` fails.

## Edge cases and gotchas

- **No limit, no meaning.** Without a memory limit, "percent of limit" is percent of the host. Set limits for containers you want to alert on.
- **Docker CPU percent can exceed 100.** It counts per core, so 200 percent means two cores busy.
- **Metric labels vary by setup.** Container names appear in `name` or `container_label_*` depending on the runtime and flags. Check your own `/metrics` output.
- **cAdvisor is heavy on busy hosts.** It adds its own CPU and memory use, so watch it and consider fewer collected metrics on tiny servers.
- **Privileged container.** Treat the host as trusted-only and keep the cAdvisor image and its port locked down.
- **Short-lived containers.** One-off jobs vanish before a scrape. Their series stop, so a missing container is not always a failure.

## Troubleshooting

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| `Cannot connect to the Docker daemon at unix:///var/run/docker.sock` | Docker not running or no permission | Start Docker and use sudo or the docker group |
| `docker stats` shows `--` for values | Container just started or stopped | Wait a few seconds and run it again |
| cAdvisor target `DOWN` with `connection refused` | Wrong address or container not up | Check `docker ps` and the target address |
| No `name` label on metrics | Docker data mount missing | Mount `/var/lib/docker` read-only as in the example |
| `bind: address already in use` on 8080 | Another service uses 8080 | Choose another host port, see [fix port already in use](https://levelrail.com/learn/troubleshooting/fix-port-already-in-use/)) |
| Metrics for containers are empty | cgroup layout not readable | Check the README for your OS and cgroup version |
| Port 8080 reachable from the internet | Published on all interfaces | Bind to `127.0.0.1` and check the firewall |
| Memory seems too high | Using raw usage including cache | Use `container_memory_working_set_bytes` |

## Quick reference

```bash
docker stats --no-stream
docker stats --no-stream --format "{{.Name}} {{.CPUPerc}} {{.MemUsage}}"
curl -s http://127.0.0.1:8080/metrics | grep container_memory_working_set_bytes
```

```text
rate(container_cpu_usage_seconds_total{name!=""}[5m])
container_memory_working_set_bytes{name!=""}
```

## Related lessons

- [Prometheus and node exporter basics](https://levelrail.com/learn/monitoring/prometheus-and-node-exporter-basics/)): the scraper this plugs into.
- [Disk, memory and CPU alerts that matter](https://levelrail.com/learn/monitoring/disk-memory-and-cpu-alerts-that-matter/)): host alerts that point at the container.
- [Fix a Docker container that keeps restarting](https://levelrail.com/learn/troubleshooting/fix-a-docker-container-that-keeps-restarting/)): a common reason for a container to disappear from graphs.
- [Limit Docker log size](https://levelrail.com/learn/deploy-operate/limit-docker-log-size/)): the other way containers fill a disk.
- [Publish container ports safely](https://levelrail.com/learn/docker/publish-container-ports-safely/)): keep cAdvisor private.

Related post: [Docker container restarting in a loop](https://levelrail.com/blog/docker-container-restarting-in-a-loop/)).

## Next

Show users the result in [build a public status page](https://levelrail.com/learn/monitoring/build-a-public-status-page/)).

## Frequently asked questions

### How do I see CPU and memory per container?

Run docker stats. It prints a live table with CPU percent, memory usage and limit, network and block I/O for every running container. Add --no-stream for a single snapshot.

### How do I get docker stats output once for a script?

Use docker stats --no-stream --format "{{.Name}} {{.CPUPerc}} {{.MemUsage}}". The --format flag takes a Go template with the columns you want.

### What is cAdvisor?

cAdvisor is a Google-developed container advisor that reads usage from the host and exposes per-container metrics at /metrics for Prometheus.

### Which cAdvisor metric shows container memory?

container_memory_working_set_bytes is the usual one for memory pressure, since it excludes reclaimable cache. For CPU use rate on container_cpu_usage_seconds_total.


## Sources

- [docker stats reference](https://docs.docker.com/reference/cli/docker/container/stats/)
- [cAdvisor on GitHub](https://github.com/google/cadvisor)
