{"id":323024,"date":"2021-08-12T15:00:29","date_gmt":"2021-08-12T12:00:29","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-monitor-the-resource-usage-of-docker-containers-cloudsavvy-it\/"},"modified":"2021-08-12T15:00:29","modified_gmt":"2021-08-12T12:00:29","slug":"how-to-monitor-the-resource-usage-of-docker-containers-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-the-resource-usage-of-docker-containers-cloudsavvy-it\/","title":{"rendered":"#How to Monitor the Resource Usage of Docker Containers \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a41686b3b819\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a41686b3b819\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-the-resource-usage-of-docker-containers-cloudsavvy-it\/#The_Docker_Stats_Command\" >The Docker Stats Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-the-resource-usage-of-docker-containers-cloudsavvy-it\/#Getting_More_Info\" >Getting More Info<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-the-resource-usage-of-docker-containers-cloudsavvy-it\/#Finding_Resource_Metrics_With_the_Docker_API\" >Finding Resource Metrics With the Docker API<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-the-resource-usage-of-docker-containers-cloudsavvy-it\/#Viewing_Running_Processes\" >Viewing Running Processes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-the-resource-usage-of-docker-containers-cloudsavvy-it\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Monitor the Resource Usage of Docker Containers \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-9034\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/6dc7b5a0.jpeg?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/6dc7b5a0.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/6dc7b5a0.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1602\" height=\"902\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>While Docker is much lighter weight than traditional VMs, too many containers can quickly consume your host\u2019s resources. Here\u2019s how to check hardware utilization and monitor the process counts inside your containers.<\/p>\n<h2 id=\"the-docker-stats-command\"><span class=\"ez-toc-section\" id=\"The_Docker_Stats_Command\"><\/span>The Docker Stats Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker\u2019s built-in mechanism for viewing resource consumption is <code>docker stats<\/code>. This command gives you a tabulated view of your containers. Each container displays a live feed of its critical metrics.<\/p>\n<p>The command\u2019s output includes CPU consumption and a measure of each container\u2019s network and storage use during its lifetime. The Memory column shows the live memory usage as well as the memory limit configured on the container. When no limit is set, you\u2019ll see the amount of RAM available on your host. The final column, <code>PIDS<\/code>, is a count of the number of processes started by the container.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13716\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/08\/88feeaed.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1578\" height=\"312\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Stopped containers are excluded by default. You can add them to the table by passing the <code>-a<\/code> (<code>--all<\/code>) flag to the command. CPU and memory use will be unavailable but you\u2019ll be able to see the metrics that are aggregated through the container\u2019s life, such as network activity.<\/p>\n<p>You can view the stats of single and multiple containers in the same way as other common <code>docker<\/code> CLI commands. Pass a list of space-separated container IDs or names. The output will show the metrics for the specified containers, removing everything else.<\/p>\n<pre>docker stats first-container second-container<\/pre>\n<p><code>docker stats<\/code> supports custom formatting so you can select just the columns you need. The <code>--format<\/code> flag accepts a Go <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/engine\/reference\/commandline\/stats\/#formatting\">placeholder string<\/a> that lets you create custom data visualizations.<\/p>\n<p>Here\u2019s how to show container names with CPU and memory use metrics:<\/p>\n<pre>docker stats --format \"table {{.Name}}t{{.CPUPerc}}t{{.MemUsage}}\"<\/pre>\n<p>The <code>table<\/code> formatting type prepends column headers to the output. Omit this if you want the raw data without tabulation. If you use the same formatting string regularly, consider adding it as a shell alias for ease of access.<\/p>\n<h2 id=\"getting-more-info\"><span class=\"ez-toc-section\" id=\"Getting_More_Info\"><\/span>Getting More Info<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>More detailed information about a container\u2019s resource usage can be acquired by <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/config\/containers\/runmetrics\/#control-groups\">inspecting its control group<\/a> (cgroup). This kernel mechanism tracks the consumption of a group of processes, exposing collected metrics in a pseudo-filesystem.<\/p>\n<p>Two versions of the cgroup system are available. v2 is only supported on Docker 20.10 or later with Linux kernel v4.15. Older releases will be using v1. Documentation on v2 is still incomplete, so v1 can be easier to work with.<\/p>\n<p>To find a container\u2019s cgroup, you need to determine which version is active and know the container\u2019s full ID. This must be the complete version, not the truncated form shown in <code>docker ps<\/code> and <code>docker stats<\/code> output. You can find it by running <code>docker ps --no-trunc<\/code>.<\/p>\n<p>Combine the container ID with the path to your system\u2019s control groups directory. Paths for v1 and v2 are documented <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/config\/containers\/runmetrics\/#control-groups\">by Docker<\/a>. Then you can inspect the pseudo-filesystem to find detailed resource stats. Here\u2019s the path to find a container\u2019s memory use when using cgroups v1:<\/p>\n<pre>cat \/sys\/fs\/cgroup\/memory\/docker\/&lt;full container id&gt;\/memory.stat<\/pre>\n<p>The <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/config\/containers\/runmetrics\/#metrics-from-cgroups-memory-cpu-block-io\">memory file<\/a> provides detailed information on consumption, limits, paging, and swap use.<\/p>\n<h2 id=\"finding-resource-metrics-with-the-docker-api\"><span class=\"ez-toc-section\" id=\"Finding_Resource_Metrics_With_the_Docker_API\"><\/span>Finding Resource Metrics With the Docker API<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A more straightforward way of accessing this information is via the Docker API. This is enabled by default via the Docker daemon\u2019s Unix socket. The <code>\/containers\/{id}\/stats<\/code> endpoint provides in-depth resource utilization details. Replace <code>{id}<\/code> with your container\u2019s ID.<\/p>\n<pre>curl --unix-socket \/var\/run\/docker.sock \"http:\/\/localhost\/v1.41\/containers\/{id}\/stats\" | jq<\/pre>\n<p>We\u2019re using <code>curl<\/code> in this example. It\u2019s instructed to use the Docker daemon socket via the <code>--unix-socket<\/code> flag. The Docker API will return data in JSON format; this is piped into <code>jq<\/code> to make it more readable in the terminal.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13720\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/08\/4e6fc47e.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1279\" height=\"710\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Each <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/engine\/api\/v1.41\/#operation\/ContainerStats\">API response<\/a> contains detailed information on the container\u2019s current and past resource utilization. It\u2019s numerical data intended for consumption by machine tools. Values are presented \u201craw\u201d and may not be im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely intelligible without further processing or ingest into a dashboard tool.<\/p>\n<h2 id=\"viewing-running-processes\"><span class=\"ez-toc-section\" id=\"Viewing_Running_Processes\"><\/span>Viewing Running Processes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A separate command, <code>docker top<\/code>, lets you see the current process list of a specified container:<\/p>\n<pre>docker top my-container<\/pre>\n<p>It enumerates the container\u2019s process list at the time the command is run. Unlike <code>stats<\/code>, it does not provide a live data stream. You can see each process\u2019 ID, the user which started it, and the command that\u2019s being run.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13717\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/08\/5e6d4b7d.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"2094\" height=\"216\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You can also get this information from the API. Use the same <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>roach as described above, substituting the <code>\/containers\/{id}\/stats<\/code> endpoint for <code>\/containers\/{id}\/top<\/code>.<\/p>\n<p>Docker doesn\u2019t provide an integrated way of viewing per-process resource utilization. If you want this information, it\u2019s best to attach to the container and install <code>top<\/code> or <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/howto\/ubuntu\/using-htop-to-monitor-system-processes-on-linux\"><code>htop<\/code><\/a>. These tools will give you a much deeper view of the container\u2019s activity.<\/p>\n<pre>docker exec -it my-container sh&#13;\n&#13;\n# substitute your package manager's commands&#13;\napt update &amp;&amp; apt install htop -y&#13;\n&#13;\nhtop<\/pre>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The Docker daemon collects and exposes real-time and historical resource consumption statistics about your containers. You can access a basic graphical view of the data using <code>docker stats<\/code> but for more advanced readouts the Docker API or manual control group inspection is needed.<\/p>\n<p>You can list a container\u2019s running processes too but the <code>docker top<\/code> command does not provide any indication of resource metrics. This means it\u2019s of limited use when inspecting <em>why<\/em> a container is holding excessive CPU or memory. You\u2019ll need to attach to it manually and inspect from within.<\/p>\n<p>Docker\u2019s tools target <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a> monitoring and observability, not detailed inspection to facilitate resolution of issues. Most of the time, they\u2019re perfectly adequate but a good knowledge of wider Linux monitoring tools which work <em>inside<\/em> containers will be more effective when solving problems.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/13715\/how-to-monitor-the-resource-usage-of-docker-containers\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Monitor the Resource Usage of Docker Containers \u2013 CloudSavvy IT&#8221; While Docker is much lighter weight than traditional VMs, too many containers can quickly consume your host\u2019s resources. Here\u2019s how to check hardware utilization and monitor the process counts inside your containers. The Docker Stats Command Docker\u2019s built-in mechanism for viewing resource consumption&#8230;<\/p>\n","protected":false},"author":1,"featured_media":323025,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/6dc7b5a0.jpeg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-323024","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/323024","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=323024"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/323024\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/323025"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=323024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=323024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=323024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}