How to Check Docker Container RAM and CPU Usage

0
42

How to check Docker CPU and memory usage? In this tutorial, you will learn how to check Docker container RAM and CPU usage. Just like how you would monitor/check the resource usage on your Linux/Windows systems, it is also possible to check how much RAM or CPU percentage each of the Docker containers you have deployed is consuming. This will enable you plan resource capacity of your host server before hand.

How to Check Docker Container RAM and CPU Usage

So, how can you be able to check Docker container RAM and CPU usage? Well, Docker ships with a statistics command option that enables you easily display a live stream of container(s) runtime metrics.

The command is;

docker stats

Apart from CPU and memory usage statistics, docker stats can also show you container memory limit, and network IO metrics.

docker stats command line usage

To see command usage help, run;

docker stats --help
Usage:  docker stats [OPTIONS] [CONTAINER...]

Display a live stream of container(s) resource usage statistics

Options:
  -a, --all             Show all containers (default shows just running)
      --format string   Pretty-print images using a Go template
      --no-stream       Disable streaming stats and only pull the first result
      --no-trunc        Do not truncate output

Show Docker Containers General Metrics

In its simple usage, you can simply run the command without any option to display runtime metrics of all containers!

docker stats

The command will display a live stream of container(s) runtime metrics just like top/htop commands.

Sample output;

CONTAINER ID   NAME                CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O         PIDS
b46dce1fc33c   dozzle              0.00%     4.582MiB / 1.929GiB   0.23%     7.89kB / 2.42kB   0B / 0B           8
1421e6e27733   nagios-core-4.4.9   0.04%     31.03MiB / 1.929GiB   1.57%     26.6kB / 44.7kB   75.8MB / 8.22MB   13

You can disable the live display by passing the –no-stream option. This will display the container metrics in the moment you ran the command and exit;

docker stats --no-stream

So, what does the columns of the command output mean?

CONTAINER IDShows the container ID
NameShows the name of the container
CPU %Shows the percentage of the host’s CPU the container is using
MEM %Shows the percentage of the host RAM the container is using
MEM USAGE / LIMITShows the total RAM the container is using, and the total amount of RAM it is allowed to use
NET I/OShows the amount of data the container has sent and received over its network interface
BLOCK I/OShows the amount of data the container has read to and written from block devices on the host
PIDsShows the number of processes or threads the container has created inside the respective container

Show Resource Usage of Specific Docker Container

To show resource usage of specific Docker container using the docker stats command, simply add the Docker container name or ID to the command;

docker stats [container_ID|container_NAME]

For example;

docker stats dozzle

More than one container on same command;

docker stats dozzle nagios-core-4.4.9

Or;

docker stats b46dce1fc33c

More than one container IDs;

docker stats b46dce1fc33c 1421e6e27733

Show Specific Resource Usage of Specific Docker Container

You can also show specific resource usage such as CPU % or RAM e.t.c for a specific container using the --format option which prints the output using a Go template.

The --format options has a respective placeholder for each metric disaplayed;

PlaceholderDescription
.ContainerContainer name or ID (user input)
.NameContainer name
.IDContainer ID
.CPUPercCPU percentage
.MemUsageMemory usage
.NetIONetwork IO
.BlockIOBlock IO
.MemPercMemory percentage (Not available on Windows)
.PIDsNumber of PIDs (Not available on Windows)

You would use the –format option as follows;

docker stats --format "{{.Container}}[delimeter]{{.Placeholder-1}}[delimeter]{{.Placeholder-2}}"
  • [delimeter] can be anything, space ( ), tab (\t), pipe (|), comma (,), colon (:) e.t.c.
  • {{.Placeholder-N}} could be any of the above placeholders

To display CPU usage of all containers;

docker stats --format "{{.Container}} {{.CPUPerc}}" --no-stream

Sample output;

b46dce1fc33c 0.00%
1421e6e27733 0.04%

For specific container;

docker stats --format "{{.Container}} {{.CPUPerc}}" --no-stream <container-name|ID>

e.g;

docker stats --format "{{.Container}} {{.CPUPerc}}" --no-stream dozzle

Sample output;

dozzle:0.00%

As you can see, the result is separated by space delimiter. However, there is no column header names.

To show the names of the columns, pass the table option to the command;

docker stats --format "table {{.Container}}[delimeter]{{.Placeholder-1}}[delimeter]{{.Placeholder-2}}"

For example, to show tab delimited results of CPU usage, RAM Usage of all containers;

docker stats --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemPerc}}"

Sample output;

CONTAINER      CPU %     MEM %
b46dce1fc33c   0.00%     0.23%
1421e6e27733   0.02%     1.58%

If you don’t want to display container ID or Name from the stats output, omit {{.Container}} option;

docker stats --format "{{.MemPerc}}" <container-name|ID>

e.g

docker stats --no-stream --format "{{.MemPerc}}" dozzle

Sample output;

0.23%

Show Docker Container Resource Usage from Pseudofiles

It is also possible to check Docker container metrics from the control groups which are exposed through Pseudo-filesystems. Control groups are located under the /sys/fs/cgroup directory on the Docker container host system.

The metric related to memory, CPU, block I/O for each specific Docker container are exposed through pseudo-file such as memory.stat, cpu.stat, io.stat respectively under the /sys/fs/cgroup/system.slice/docker-LONG_CONTAINER_ID.scope/ directory

To get long Docker container IDs;

docker ps --no-trunc --format "{{.Names}}\t{{.ID}}"

Sample output;

dozzle	b46dce1fc33c05232120a8b317785b13aaf749728e70a17cb3d4c32366ad4c77
nagios-core-4.4.9	1421e6e27733bf9c60378fe5b435528bfbe33af75c2b0abd2500a2281eb2a4f8

Thus, you can get individual container metrics for example;

cat /sys/fs/cgroup/system.slice/docker-b46dce1fc33c05232120a8b317785b13aaf749728e70a17cb3d4c32366ad4c77.scope/cpu.stat
usage_usec 466029
user_usec 425019
system_usec 41009
nr_periods 0
nr_throttled 0
throttled_usec 0
cat /sys/fs/cgroup/system.slice/docker-b46dce1fc33c05232120a8b317785b13aaf749728e70a17cb3d4c32366ad4c77.scope/memory.stat
anon 4370432
file 0
kernel_stack 131072
pagetables 98304
percpu 72
sock 0
shmem 0
file_mapped 0
file_dirty 0
file_writeback 0
swapcached 0
anon_thp 0
file_thp 0
shmem_thp 0
inactive_anon 4366336
active_anon 4096
inactive_file 0
active_file 0
unevictable 0
slab_reclaimable 71688
slab_unreclaimable 124968
slab 196656
workingset_refault_anon 0
workingset_refault_file 0
workingset_activate_anon 0
workingset_activate_file 0
workingset_restore_anon 0
workingset_restore_file 0
workingset_nodereclaim 0
pgfault 3029
pgmajfault 0
pgrefill 0
pgscan 0
pgsteal 0
pgactivate 0
pgdeactivate 0
pglazyfree 0
pglazyfreed 0
thp_fault_alloc 0
thp_collapse_alloc 0

And that is it on how to check Docker container RAM and CPU usage.

Reference

docker stats

Other Tutorials

How to Install Docker Resource Usage Extension

Monitor Changes to Critical Files on Windows Systems using Wazuh and ELK

How to use htop Command in Linux

How to Monitor Disk Input/Output on Linux

How to Measure CPU Usage in Linux

LEAVE A REPLY

Please enter your comment!
Please enter your name here