Docker - Checking Container Resource Usage
Learn how to analyze a docker container's resource usage using the docker stats command. We will also go over configuration options and how to format the output of the command.
Table of Contents 📖
The docker stats Command
We can see a live stream of a container's resource usage statistics using the docker stats command. This command returns information such as the CPU usage, memory usage, etc.
docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
5b9903d3aa37 wb-server-c 0.00% 108.1MiB / 1GiB 10.55% 54.8MB / 35.8MB 712MB / 13.3MB 22
f53ad63cfbf9 wb-postgres-c 0.00% 21.03MiB / 1GiB 2.05% 2.24MB / 2.4MB 322MB / 22.3MB 6
7d96f9c49439 wb-cron-c 0.00% 10.98MiB / 1GiB 1.07% 1.57MB / 12.9MB 713MB / 47.1GB 2
a26c17b9d310 wittcode.com 0.00% 5.652MiB / 1GiB 0.55% 485MB / 18GB 2.18GB / 24.6kB 2
INFO: By default, the docker stats command will return data for all running containers.
Here is a summary of all the fields:
- CONTAINER ID - The ID of the container.
- NAME - The name of the container.
- CPU % - The percentage of the host's CPU the container is using.
- MEM USAGE / LIMIT - The amount of memory the container is using out of the total memory the container is allowed to use.
- MEM % - The percentage of the host's memory the container is using.
- NET I/O - The amount of data the container has received and sent over its network interface.
- BLOCK I/O - The amount of data the container has written to and read from block devices on the host.
- PIDs - The number of processes/threads the container has created.
To get information on a specific container or containers, provide a list of the container names or IDs separated by a space.
docker stats wb-server-c wb-cron-c
docker stats Options
We can configure this command by supplying options. One option is --no-stream which displays the first result of the container's usage data stream instead of a constantly updating stream.
docker stats --no-stream
We can also list every container, not just the ones running, by tagging on -a or --all.
docker stats --all
We can also format the output with the --format flag.
docker stats --format "{{.Name}} {{.MemUsage}}"
wb-server-c 107.9MiB / 1GiB
wb-postgres-c 21.11MiB / 1GiB
wb-cron-c 70.46MiB / 1GiB
wittcode.com 6.938MiB / 1GiB
The values provided to the format flag can be found at the following URL:
https://docs.docker.com/reference/cli/docker/container/stats/