{"id":15212,"date":"2023-01-16T21:15:54","date_gmt":"2023-01-16T18:15:54","guid":{"rendered":"https:\/\/kifarunix.com\/?p=15212"},"modified":"2024-03-09T23:52:21","modified_gmt":"2024-03-09T20:52:21","slug":"how-to-check-docker-container-ram-and-cpu-usage","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/how-to-check-docker-container-ram-and-cpu-usage\/","title":{"rendered":"How to Check Docker Container RAM and CPU Usage"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1057\" height=\"592\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/check-docker-container-resource-usage.png\" alt=\"check Docker container RAM and CPU usage\" class=\"wp-image-15218\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/check-docker-container-resource-usage.png 1057w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/check-docker-container-resource-usage-768x430.png 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/check-docker-container-resource-usage-150x84.png 150w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/check-docker-container-resource-usage-300x168.png 300w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/check-docker-container-resource-usage-696x390.png 696w, https:\/\/kifarunix.com\/wp-content\/uploads\/2023\/01\/check-docker-container-resource-usage-750x420.png 750w\" sizes=\"(max-width: 1057px) 100vw, 1057px\" \/><\/figure>\n\n\n\n<p>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 <a href=\"https:\/\/docs.docker.com\/config\/containers\/runmetrics\/#docker-stats\" target=\"_blank\" rel=\"noreferrer noopener\">Docker<\/a> containers you have deployed is consuming. This will enable you plan resource capacity of your host server before hand.<\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#checking-docker-container-ram-and-cpu-usage\">Checking Docker Container RAM and CPU Usage<\/a><ul><li><a href=\"#docker-stats-command-line-usage\">docker stats command line usage<\/a><\/li><li><a href=\"#show-docker-containers-general-metrics\">Show Docker Containers General Metrics<\/a><\/li><li><a href=\"#show-resource-usage-of-specific-docker-container\">Show Resource Usage of Specific Docker Container<\/a><\/li><li><a href=\"#show-specific-resource-usage-of-specific-docker-container\">Show Specific Resource Usage of Specific Docker Container<\/a><\/li><li><a href=\"#show-docker-container-resource-usage-from-pseudofiles\">Show Docker Container Resource Usage from Pseudofiles<\/a><\/li><li><a href=\"#reference\">Reference<\/a><\/li><li><a href=\"#other-tutorials\">Other Tutorials<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"checking-docker-container-ram-and-cpu-usage\">Checking Docker Container RAM and CPU Usage<\/h2>\n\n\n\n<p>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) <strong>runtime<\/strong> metrics.<\/p>\n\n\n\n<p>The command is;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stats<\/code><\/pre>\n\n\n\n<p>Apart from CPU and memory usage statistics, <code>docker stats<\/code> can also show you container memory limit, and network IO metrics.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"docker-stats-command-line-usage\"><strong><code>docker stats<\/code><\/strong> command line usage<\/h3>\n\n\n\n<p>To see command usage help, run;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stats --help<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>Usage:  docker stats [OPTIONS] [CONTAINER...]\n\nDisplay a live stream of container(s) resource usage statistics\n\nOptions:\n  -a, --all             Show all containers (default shows just running)\n      --format string   Pretty-print images using a Go template\n      --no-stream       Disable streaming stats and only pull the first result\n      --no-trunc        Do not truncate output\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"show-docker-containers-general-metrics\">Show Docker Containers General Metrics<\/h3>\n\n\n\n<p>In its simple usage, you can simply run the command without any option to display runtime metrics of all containers!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stats<\/code><\/pre>\n\n\n\n<p>The command will display a live stream of container(s) <strong>runtime<\/strong> metrics just like <strong><code>top\/htop<\/code><\/strong> commands.<\/p>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"scroll-sz\"><code>CONTAINER ID   NAME                CPU %     MEM USAGE \/ LIMIT     MEM %     NET I\/O           BLOCK I\/O         PIDS\nb46dce1fc33c   dozzle              0.00%     4.582MiB \/ 1.929GiB   0.23%     7.89kB \/ 2.42kB   0B \/ 0B           8\n1421e6e27733   nagios-core-4.4.9   0.04%     31.03MiB \/ 1.929GiB   1.57%     26.6kB \/ 44.7kB   75.8MB \/ 8.22MB   13\n<\/code><\/pre>\n\n\n\n<p>You can disable the live display by passing the &#8211;no-stream option. This will display the container metrics in the moment you ran the command and exit;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stats --no-stream<\/code><\/pre>\n\n\n\n<p>So, what does the columns of the command output mean?<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><code>CONTAINER ID<\/code><\/td><td>Shows the container ID<\/td><\/tr><tr><td><code>Name<\/code><\/td><td>Shows the name of the container<\/td><\/tr><tr><td><code>CPU %<\/code><\/td><td>Shows the percentage of the host\u2019s CPU the container is using<\/td><\/tr><tr><td><code>MEM %<\/code><\/td><td>Shows the percentage of the host RAM the container is using<\/td><\/tr><tr><td><code>MEM USAGE \/ LIMIT<\/code><\/td><td>Shows the total RAM the container is using, and the total amount of RAM it is allowed to use<\/td><\/tr><tr><td><code>NET I\/O<\/code><\/td><td>Shows the amount of data the container has sent and received over its network interface<\/td><\/tr><tr><td><code>BLOCK I\/O<\/code><\/td><td>Shows the amount of data the container has read to and written from block devices on the host<\/td><\/tr><tr><td><code>PIDs<\/code><\/td><td>Shows the number of processes or threads the container has created inside the respective container<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"show-resource-usage-of-specific-docker-container\">Show Resource Usage of Specific Docker Container<\/h3>\n\n\n\n<p>To show resource usage of specific Docker container using the <strong><code>docker stats<\/code><\/strong> command, simply add the Docker container name or ID to the command;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stats &#91;container_ID|container_NAME]<\/code><\/pre>\n\n\n\n<p>For example;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stats dozzle<\/code><\/pre>\n\n\n\n<p>More than one container on same command;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stats dozzle nagios-core-4.4.9<\/code><\/pre>\n\n\n\n<p>Or;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stats b46dce1fc33c<\/code><\/pre>\n\n\n\n<p>More than one  container IDs;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stats b46dce1fc33c 1421e6e27733<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"show-specific-resource-usage-of-specific-docker-container\">Show Specific Resource Usage of Specific Docker Container<\/h3>\n\n\n\n<p>You can also show specific resource usage such as CPU % or RAM e.t.c for a specific container using the <strong><code>--format<\/code><\/strong> option which prints the output using a Go template.<\/p>\n\n\n\n<p>The <strong><code>--format<\/code><\/strong> options has a respective placeholder for each metric disaplayed;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Placeholder<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>.Container<\/code><\/td><td>Container name or ID (user input)<\/td><\/tr><tr><td><code>.Name<\/code><\/td><td>Container name<\/td><\/tr><tr><td><code>.ID<\/code><\/td><td>Container ID<\/td><\/tr><tr><td><code>.CPUPerc<\/code><\/td><td>CPU percentage<\/td><\/tr><tr><td><code>.MemUsage<\/code><\/td><td>Memory usage<\/td><\/tr><tr><td><code>.NetIO<\/code><\/td><td>Network IO<\/td><\/tr><tr><td><code>.BlockIO<\/code><\/td><td>Block IO<\/td><\/tr><tr><td><code>.MemPerc<\/code><\/td><td>Memory percentage (Not available on Windows)<\/td><\/tr><tr><td><code>.PIDs<\/code><\/td><td>Number of PIDs (Not available on Windows)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>You would use the &#8211;format option as follows;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stats --format \"{{.Container}}&#91;delimeter]{{.Placeholder-1}}&#91;delimeter]{{.Placeholder-2}}\"<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>[delimeter] can be anything, space ( ), tab (\\t), pipe (|), comma (,), colon (:) e.t.c.<\/li>\n\n\n\n<li>{{.Placeholder-<strong>N<\/strong>}} could be any of the above placeholders<\/li>\n<\/ul>\n\n\n\n<p>To display CPU usage of all containers;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stats --format \"{{.Container}} {{.CPUPerc}}\" --no-stream<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>b46dce1fc33c 0.00%\n1421e6e27733 0.04%<\/code><\/pre>\n\n\n\n<p>For specific container;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stats --format \"{{.Container}} {{.CPUPerc}}\" --no-stream &lt;container-name|ID&gt;<\/code><\/pre>\n\n\n\n<p>e.g;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stats --format \"{{.Container}} {{.CPUPerc}}\" --no-stream dozzle<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dozzle:0.00%<\/code><\/pre>\n\n\n\n<p>As you can see, the result is separated by space delimiter. However, there is no column header names.<\/p>\n\n\n\n<p>To show the names of the columns, pass the <strong>table<\/strong> option to the command;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stats --format \"table {{.Container}}&#91;delimeter]{{.Placeholder-1}}&#91;delimeter]{{.Placeholder-2}}\"<\/code><\/pre>\n\n\n\n<p>For example, to show tab delimited results of CPU usage, RAM Usage of all containers;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stats --format \"table {{.Container}}\\t{{.CPUPerc}}\\t{{.MemPerc}}\"<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CONTAINER      CPU %     MEM %\nb46dce1fc33c   0.00%     0.23%\n1421e6e27733   0.02%     1.58%<\/code><\/pre>\n\n\n\n<p>If you don&#8217;t want to display container ID or Name from the stats output, omit {{.Container}} option;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stats --format \"{{.MemPerc}}\" &lt;container-name|ID&gt;<\/code><\/pre>\n\n\n\n<p>e.g<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker stats --no-stream --format \"{{.MemPerc}}\" dozzle<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0.23%<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"show-docker-container-resource-usage-from-pseudofiles\">Show Docker Container Resource Usage from Pseudofiles<\/h3>\n\n\n\n<p>It is also possible to check Docker container metrics from the <a href=\"https:\/\/www.kernel.org\/doc\/Documentation\/cgroup-v1\/cgroups.txt\" target=\"_blank\" rel=\"noreferrer noopener\">control groups<\/a> which are exposed through Pseudo-filesystems. Control groups are located under the <strong><code>\/sys\/fs\/cgroup<\/code><\/strong> directory on the Docker container host system.<\/p>\n\n\n\n<p>The metric related to memory, CPU, block I\/O for each specific Docker container are exposed through pseudo-file such as <strong><code>memory.stat<\/code><\/strong>, <code>cpu.stat<\/code>, <code>io.stat<\/code> respectively under the <strong><code>\/sys\/fs\/cgroup\/system.slice\/docker-LONG_CONTAINER_ID.scope\/<\/code><\/strong> directory<\/p>\n\n\n\n<p>To get long Docker container IDs;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker ps --no-trunc --format \"{{.Names}}\\t{{.ID}}\"<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dozzle\tb46dce1fc33c05232120a8b317785b13aaf749728e70a17cb3d4c32366ad4c77\nnagios-core-4.4.9\t1421e6e27733bf9c60378fe5b435528bfbe33af75c2b0abd2500a2281eb2a4f8<\/code><\/pre>\n\n\n\n<p>Thus, you can get individual container metrics for example;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/sys\/fs\/cgroup\/system.slice\/docker-<strong>b46dce1fc33c05232120a8b317785b13aaf749728e70a17cb3d4c32366ad4c77<\/strong>.scope\/cpu.stat<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>usage_usec 466029\nuser_usec 425019\nsystem_usec 41009\nnr_periods 0\nnr_throttled 0\nthrottled_usec 0<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/sys\/fs\/cgroup\/system.slice\/docker-<strong>b46dce1fc33c05232120a8b317785b13aaf749728e70a17cb3d4c32366ad4c77<\/strong>.scope\/memory.stat<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>anon 4370432\nfile 0\nkernel_stack 131072\npagetables 98304\npercpu 72\nsock 0\nshmem 0\nfile_mapped 0\nfile_dirty 0\nfile_writeback 0\nswapcached 0\nanon_thp 0\nfile_thp 0\nshmem_thp 0\ninactive_anon 4366336\nactive_anon 4096\ninactive_file 0\nactive_file 0\nunevictable 0\nslab_reclaimable 71688\nslab_unreclaimable 124968\nslab 196656\nworkingset_refault_anon 0\nworkingset_refault_file 0\nworkingset_activate_anon 0\nworkingset_activate_file 0\nworkingset_restore_anon 0\nworkingset_restore_file 0\nworkingset_nodereclaim 0\npgfault 3029\npgmajfault 0\npgrefill 0\npgscan 0\npgsteal 0\npgactivate 0\npgdeactivate 0\npglazyfree 0\npglazyfreed 0\nthp_fault_alloc 0\nthp_collapse_alloc 0\n<\/code><\/pre>\n\n\n\n<p>And that is it on how to check Docker container RAM and CPU usage.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"reference\">Reference<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><a href=\"https:\/\/docs.docker.com\/engine\/reference\/commandline\/stats\/\" target=\"_blank\" rel=\"noopener\">docker stats<\/a><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"other-tutorials\">Other Tutorials<\/h3>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-install-docker-resource-usage-extension\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Install Docker Resource Usage Extension<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/monitor-changes-to-critical-files-on-windows-systems-using-wazuh-and-elk\/\" target=\"_blank\" rel=\"noreferrer noopener\">Monitor Changes to Critical Files on Windows Systems using Wazuh and ELK<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-use-htop-command-in-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to use htop Command in Linux<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-monitor-disk-input-output-on-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Monitor Disk Input\/Output on Linux<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-measure-cpu-usage-in-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Measure CPU Usage in Linux<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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<\/p>\n","protected":false},"author":10,"featured_media":15218,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[72,1076,1077,121],"tags":[6269,6273,6272,6270,6271],"class_list":["post-15212","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-monitoring","category-containers","category-docker","category-howtos","tag-check-docker-container-resource-usage","tag-display-docker-runtime-metrics","tag-docker-container-metrics","tag-how-to-use-docker-stats","tag-show-docker-container-statistics","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/15212"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=15212"}],"version-history":[{"count":10,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/15212\/revisions"}],"predecessor-version":[{"id":20683,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/15212\/revisions\/20683"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/15218"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=15212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=15212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=15212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}