Docker is one of the most popular and widely used containerization platforms for building, sharing, and running applications. In today's world, most applications are deployed on the containerization platform so managing a Docker container is the key task of any system administrator. Docker allows you to check and monitor the resource usage like CPU, and Memory usage of the Docker containers.
This tutorial will show you how to check Docker container CPU and RAM usage.
Prerequisites
- A server running Linux with Docker installed.
- A root password is configured on the server.
How to Monitor and Check Docker Container CPU and RAM Usage
Docker provides a stats sub-command that allows you to check your running Docker containers' memory and CPU utilization.
To list all docker stats commands, run the following command.
docker stats --help
You will get the following output.
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
Now, let's run the docker stats command to check the status of all running containers:
docker stats
You will get the information of all running containers in the following output.
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS cb40a0f56aba determined_robinson 0.00% 2.648MiB / 3.839GiB 0.07% 726B / 0B 0B / 12.3kB 3 e0ee5b16c93f portainer 0.84% 24.6MiB / 3.839GiB 0.63% 1.02MB / 4.35MB 0B / 721kB 5
By default, the docker stats command will display a live stream of all container stats. If you want to see the first stats of all running containers, use the --no-stream flag.
docker stats --no-stream
You will get the following output.
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS cb40a0f56aba determined_robinson 0.00% 2.648MiB / 3.839GiB 0.07% 796B / 0B 0B / 57.3kB 3 e0ee5b16c93f portainer 0.00% 25.5MiB / 3.839GiB 0.65% 1.03MB / 4.35MB 0B / 721kB 5
By default, the docker stats command will display the stats of all running containers. If you want to display the stats of both running and stopped containers, use the -a flag:
docker stats --no-stream -a
If you want to check the status of a specific container, use the docker stats command followed by container id.
docker stats cb40a0f56aba
You will get the following output.
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS cb40a0f56aba determined_robinson 0.00% 2.648MiB / 3.839GiB 0.07% 796B / 0B 0B / 57.3kB 3
Run the following command to display only CPU information of all running containers.
docker stats --format "{{.Container}} {{.CPUPerc}}" --no-stream
Output.
cb40a0f56aba 0.00% e0ee5b16c93f 0.00%
Run the following command to display only CPU and Memory information of all running containers.
docker stats --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemPerc}}"
Output.
CONTAINER CPU % MEM % cb40a0f56aba 0.00% 0.07% e0ee5b16c93f 0.10% 0.59%
Display Docker Container Usage Using Pseudofiles
You can also check Docker container metrics from the control groups which are located under the /sys/fs/cgroup directory on the Docker container host system.
First, get the all running container id using the following command.
docker ps --no-trunc --format "{{.Names}}\t{{.ID}}"
You will get the following output.
determined_robinson cb40a0f56aba8b3a29513dafa7622f8d5d6435c9302dc6a3db73ea5eca506af5 portainer e0ee5b16c93f358a15dfcb93cc80d2c2d1c1c675e7b65e41434ce20f08773465
Next, run the following command to get the stats of the second container.
cat /sys/fs/cgroup/system.slice/docker-e0ee5b16c93f358a15dfcb93cc80d2c2d1c1c675e7b65e41434ce20f08773465.scope/cpu.stat
You will get the container metrics in the following output.
usage_usec 1550758 user_usec 1301875 system_usec 248883 nr_periods 0 nr_throttled 0 throttled_usec 0
Conclusion
In this post, we showed you how to check Docker container CPU and Memory usage via the command line. I hope this will help you to monitor your Docker environment. Feel free to ask me if you have any questions.