{"id":48413,"date":"2025-02-08T08:27:31","date_gmt":"2025-02-08T08:27:31","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=48413"},"modified":"2025-07-12T05:38:43","modified_gmt":"2025-07-12T05:38:43","slug":"docker-stats","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/docker-stats\/","title":{"rendered":"Docker commands Guide &#8211; docker stats with examples"},"content":{"rendered":"\n<p>Here\u2019s a <strong>complete tutorial<\/strong> on <code>docker stats<\/code>, covering <strong>what it does<\/strong>, a <strong>comprehensive list of examples<\/strong>, and <strong>use cases<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is <code>docker stats<\/code>?<\/strong><\/h2>\n\n\n\n<p><code>docker stats<\/code> is a Docker command that provides <strong>real-time resource usage statistics<\/strong> for running containers, such as <strong>CPU, memory, network I\/O, and block I\/O<\/strong>. It\u2019s useful for monitoring the performance of your containers and diagnosing resource-related issues.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Key Features:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Displays <strong>live resource usage<\/strong> for one or more containers.<\/li>\n\n\n\n<li>Helps in <strong>performance monitoring<\/strong> and <strong>resource optimization<\/strong>.<\/li>\n\n\n\n<li>Works similarly to the <strong><code>top<\/code> command<\/strong> in Linux but is container-specific.<\/li>\n\n\n\n<li>Supports <strong>customized output<\/strong> and <strong>filtering<\/strong>.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Basic Syntax<\/strong><\/h2>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">docker<\/span> <span class=\"hljs-selector-tag\">stats<\/span> <span class=\"hljs-selector-attr\">&#91;OPTIONS]<\/span> <span class=\"hljs-selector-attr\">&#91;CONTAINER...]<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong>Options:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>--all<\/code>, <code>-a<\/code>: Show stats for <strong>all containers<\/strong>, not just running ones.<\/li>\n\n\n\n<li><code>--format<\/code>: Customize the output using Go templates.<\/li>\n\n\n\n<li><code>--no-stream<\/code>: Show <strong>only a single snapshot<\/strong> of stats, instead of continuous updates.<\/li>\n\n\n\n<li><code>--no-trunc<\/code>: Do not truncate output (show full container IDs and names).<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Examples of <code>docker stats<\/code><\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Show Real-Time Stats for All Running Containers<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker stats\n<\/code><\/span><\/pre>\n\n\n<p><strong>Example Output:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">CONTAINER ID   NAME        CPU %     MEM USAGE \/ LIMIT   MEM %     NET I\/O   BLOCK I\/O   PIDS\na1b2c3d4e5f6   my_app      2.50%     512MiB \/ 1GiB      50.00%    1.2kB\/1kB  0B\/0B       5\nb2c3d4e5f67g   my_db       0.75%     256MiB \/ 512MiB    50.00%    5kB\/5kB   0B\/0B       3\n<\/code><\/span><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Show Stats for a Specific Container<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker stats my_app\n<\/code><\/span><\/pre>\n\n\n<p>This displays live stats only for <code>my_app<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Show a Single Snapshot of Stats<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker stats --no-stream\n<\/code><\/span><\/pre>\n\n\n<p>This displays container stats once and exits.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Show Stats for Multiple Containers<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker stats my_app my_db\n<\/code><\/span><\/pre>\n\n\n<p>Displays real-time stats for both <code>my_app<\/code> and <code>my_db<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Show Stats for All Containers (Including Stopped)<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker stats --all\n<\/code><\/span><\/pre>\n\n\n<p>This includes stats for stopped containers, showing <code>0%<\/code> CPU and memory usage for them.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. Customize Output with <code>--format<\/code><\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">docker stats --format <span class=\"hljs-string\">\"table {{.Name}}\\t{{.CPUPerc}}\\t{{.MemUsage}}\"<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Example Output:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">NAME       CPU %     MEM USAGE\nmy_app     1.25%     512MiB \/ 1GiB\nmy_db      0.50%     256MiB \/ 512MiB\n<\/code><\/span><\/pre>\n\n\n<p>This formats the output to show only container name, CPU percentage, and memory usage.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. Show Full Container IDs and Names<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">docker stats --no-trunc\n<\/code><\/span><\/pre>\n\n\n<p>Prevents truncation of container IDs and names.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8. Monitor Resource Usage in a Script<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-meta\">#!\/bin\/bash<\/span>\n<span class=\"hljs-keyword\">while<\/span> <span class=\"hljs-literal\">true<\/span>; <span class=\"hljs-keyword\">do<\/span>\n  docker stats --no-stream &gt;&gt; resource_usage.log\n  sleep <span class=\"hljs-number\">60<\/span>\ndone\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This script logs resource usage for all containers every minute.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9. Monitor Specific Resource Usage for a Container<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">docker stats my_web_container --no-stream --format <span class=\"hljs-string\">\"CPU: {{.CPUPerc}}, Memory: {{.MemUsage}}\"<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Example Output:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTTP\" data-shcb-language-slug=\"http\"><span><code class=\"hljs language-http\"><span class=\"hljs-attribute\">CPU<\/span>: 2.00%, Memory: 300MiB \/ 1GiB\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTTP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">http<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>10. Combine with Other Docker Commands<\/strong><\/h3>\n\n\n\n<p>Restart a container if its memory usage exceeds a threshold:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">if<\/span> &#91;&#91; $(docker stats my_app --no-stream --format <span class=\"hljs-string\">\"{{.MemPerc}}\"<\/span> | cut -d<span class=\"hljs-string\">'%'<\/span> -f1) &gt; <span class=\"hljs-number\">80<\/span> ]]; then\n  docker restart my_app\nfi\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This checks if the container\u2019s memory usage is above 80% and restarts it if true.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Use Cases for <code>docker stats<\/code><\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Performance Monitoring<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Continuously monitor CPU, memory, and I\/O usage for critical services.<\/li>\n\n\n\n<li>Identify resource bottlenecks in multi-container environments.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Debugging Resource-Related Issues<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Diagnose high memory or CPU usage that could cause containers to slow down or crash.<\/li>\n\n\n\n<li>Example: Find containers with abnormal CPU spikes.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Capacity Planning<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use historical stats to plan resource allocation for your containers.<\/li>\n\n\n\n<li>Example: Increase memory allocation for a database container if its usage frequently reaches the limit.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Automation and Scripting<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Automate actions like scaling services, restarting containers, or triggering alerts based on resource thresholds.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Security and Incident Response<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Monitor suspicious behavior in containers (e.g., unexpected CPU or network spikes).<\/li>\n\n\n\n<li>Example: Detect a compromised container generating unusual network traffic.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. CI\/CD Pipelines<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <code>docker stats<\/code> to monitor resource usage during builds and tests to avoid exceeding CI system limits.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>List of Common <code>docker stats<\/code> Commands<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>Command<\/strong><\/th><th><strong>Description<\/strong><\/th><\/tr><\/thead><tbody><tr><td><code>docker stats<\/code><\/td><td>Show real-time stats for all running containers<\/td><\/tr><tr><td><code>docker stats my_container<\/code><\/td><td>Show stats for a specific container<\/td><\/tr><tr><td><code>docker stats --no-stream<\/code><\/td><td>Show a single snapshot of stats<\/td><\/tr><tr><td><code>docker stats --all<\/code><\/td><td>Show stats for all containers (including stopped)<\/td><\/tr><tr><td><code>docker stats --format \"table {{.Name}} {{.CPUPerc}} {{.MemUsage}}\"<\/code><\/td><td>Customize output<\/td><\/tr><tr><td><code>docker stats --no-trunc<\/code><\/td><td>Show full container IDs and names<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Best Practices for Using <code>docker stats<\/code>:<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Monitor resource usage regularly<\/strong> to prevent performance issues.<\/li>\n\n\n\n<li><strong>Use <code>--format<\/code> for clarity<\/strong> in automation and scripts.<\/li>\n\n\n\n<li><strong>Combine with alerting tools<\/strong> (like Prometheus or Grafana) for automated resource monitoring.<\/li>\n\n\n\n<li><strong>Set resource limits (<code>--memory<\/code>, <code>--cpus<\/code>)<\/strong> for containers to avoid unexpected resource consumption.<\/li>\n\n\n\n<li><strong>Analyze historical trends<\/strong> for capacity planning.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Common Errors and Solutions<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>&#8220;No such container&#8221;<\/strong><br>\u2192 Ensure the container exists and is running. Use <code>docker ps<\/code> to verify.<\/li>\n\n\n\n<li><strong>Stats show <code>0%<\/code> CPU and memory usage<\/strong><br>\u2192 This is expected for paused or stopped containers. Use <code>docker stats --all<\/code> to confirm their status.<\/li>\n\n\n\n<li><strong>Output is too large<\/strong><br>\u2192 Use <code>--format<\/code> to filter and display only the necessary fields.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Combining <code>docker stats<\/code> with Other Tools<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Monitor with Prometheus and Grafana<\/strong> for advanced visualization and alerting.<\/li>\n\n\n\n<li><strong>Integrate with custom scripts<\/strong> for automated scaling or resource management.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example: Logging Resource Usage<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">docker stats --no-stream --format <span class=\"hljs-string\">\"CPU: {{.CPUPerc}}, Mem: {{.MemUsage}}\"<\/span> &gt;&gt; usage.log\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here\u2019s a complete tutorial on docker stats, covering what it does, a comprehensive list of examples, and use cases. What is docker stats? docker stats is a Docker command that&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[4862],"tags":[],"class_list":["post-48413","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/48413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=48413"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/48413\/revisions"}],"predecessor-version":[{"id":48415,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/48413\/revisions\/48415"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=48413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=48413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=48413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}