Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

โ€œInvest in yourself โ€” your confidence is always worth it.โ€

Explore Cosmetic Hospitals

Start your journey today โ€” compare options in one place.

Docker commands Guide – docker stats with examples

Hereโ€™s 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 provides real-time resource usage statistics for running containers, such as CPU, memory, network I/O, and block I/O. Itโ€™s useful for monitoring the performance of your containers and diagnosing resource-related issues.

Key Features:

  • Displays live resource usage for one or more containers.
  • Helps in performance monitoring and resource optimization.
  • Works similarly to the top command in Linux but is container-specific.
  • Supports customized output and filtering.

Basic Syntax

docker stats [OPTIONS] [CONTAINER...]
Code language: CSS (css)

Options:

  • --all, -a: Show stats for all containers, not just running ones.
  • --format: Customize the output using Go templates.
  • --no-stream: Show only a single snapshot of stats, instead of continuous updates.
  • --no-trunc: Do not truncate output (show full container IDs and names).

Examples of docker stats

1. Show Real-Time Stats for All Running Containers

docker stats

Example Output:

CONTAINER ID   NAME        CPU %     MEM USAGE / LIMIT   MEM %     NET I/O   BLOCK I/O   PIDS
a1b2c3d4e5f6   my_app      2.50%     512MiB / 1GiB      50.00%    1.2kB/1kB  0B/0B       5
b2c3d4e5f67g   my_db       0.75%     256MiB / 512MiB    50.00%    5kB/5kB   0B/0B       3

2. Show Stats for a Specific Container

docker stats my_app

This displays live stats only for my_app.


3. Show a Single Snapshot of Stats

docker stats --no-stream

This displays container stats once and exits.


4. Show Stats for Multiple Containers

docker stats my_app my_db

Displays real-time stats for both my_app and my_db.


5. Show Stats for All Containers (Including Stopped)

docker stats --all

This includes stats for stopped containers, showing 0% CPU and memory usage for them.


6. Customize Output with --format

docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"
Code language: JavaScript (javascript)

Example Output:

NAME       CPU %     MEM USAGE
my_app     1.25%     512MiB / 1GiB
my_db      0.50%     256MiB / 512MiB

This formats the output to show only container name, CPU percentage, and memory usage.


7. Show Full Container IDs and Names

docker stats --no-trunc

Prevents truncation of container IDs and names.


8. Monitor Resource Usage in a Script

#!/bin/bash
while true; do
  docker stats --no-stream >> resource_usage.log
  sleep 60
done
Code language: JavaScript (javascript)

This script logs resource usage for all containers every minute.


9. Monitor Specific Resource Usage for a Container

docker stats my_web_container --no-stream --format "CPU: {{.CPUPerc}}, Memory: {{.MemUsage}}"
Code language: JavaScript (javascript)

Example Output:

CPU: 2.00%, Memory: 300MiB / 1GiB
Code language: HTTP (http)

10. Combine with Other Docker Commands

Restart a container if its memory usage exceeds a threshold:

if [[ $(docker stats my_app --no-stream --format "{{.MemPerc}}" | cut -d'%' -f1) > 80 ]]; then
  docker restart my_app
fi
Code language: JavaScript (javascript)

This checks if the containerโ€™s memory usage is above 80% and restarts it if true.


Use Cases for docker stats

1. Performance Monitoring

  • Continuously monitor CPU, memory, and I/O usage for critical services.
  • Identify resource bottlenecks in multi-container environments.

2. Debugging Resource-Related Issues

  • Diagnose high memory or CPU usage that could cause containers to slow down or crash.
  • Example: Find containers with abnormal CPU spikes.

3. Capacity Planning

  • Use historical stats to plan resource allocation for your containers.
  • Example: Increase memory allocation for a database container if its usage frequently reaches the limit.

4. Automation and Scripting

  • Automate actions like scaling services, restarting containers, or triggering alerts based on resource thresholds.

5. Security and Incident Response

  • Monitor suspicious behavior in containers (e.g., unexpected CPU or network spikes).
  • Example: Detect a compromised container generating unusual network traffic.

6. CI/CD Pipelines

  • Use docker stats to monitor resource usage during builds and tests to avoid exceeding CI system limits.

List of Common docker stats Commands

CommandDescription
docker statsShow real-time stats for all running containers
docker stats my_containerShow stats for a specific container
docker stats --no-streamShow a single snapshot of stats
docker stats --allShow stats for all containers (including stopped)
docker stats --format "table {{.Name}} {{.CPUPerc}} {{.MemUsage}}"Customize output
docker stats --no-truncShow full container IDs and names

Best Practices for Using docker stats:

  1. Monitor resource usage regularly to prevent performance issues.
  2. Use --format for clarity in automation and scripts.
  3. Combine with alerting tools (like Prometheus or Grafana) for automated resource monitoring.
  4. Set resource limits (--memory, --cpus) for containers to avoid unexpected resource consumption.
  5. Analyze historical trends for capacity planning.

Common Errors and Solutions

  1. “No such container”
    โ†’ Ensure the container exists and is running. Use docker ps to verify.
  2. Stats show 0% CPU and memory usage
    โ†’ This is expected for paused or stopped containers. Use docker stats --all to confirm their status.
  3. Output is too large
    โ†’ Use --format to filter and display only the necessary fields.

Combining docker stats with Other Tools

  1. Monitor with Prometheus and Grafana for advanced visualization and alerting.
  2. Integrate with custom scripts for automated scaling or resource management.

Example: Logging Resource Usage

docker stats --no-stream --format "CPU: {{.CPUPerc}}, Mem: {{.MemUsage}}" >> usage.log
Code language: JavaScript (javascript)

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

Docker Tutorials: Docker Image – Understanding Dockerfiles instructions & options

Hereโ€™s a step-by-step tutorial for Dockerfile, including explanations and examples for each major command. Dockerfile Tutorial A Dockerfile is a text file containing instructions to build a…

Read More

Docker Tutorials: Docker Image – Example and Sample Programs of Dockerfile

Reference Rajesh Kumar Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories…

Read More

Docker Tutorials: Installation and Configurations

Docker Installation in Centos/RHEL Method -1: How to install Docker Community Edition via YUM? Step 1 – Install required packages. yum-utils provides the yum-config-manager utility, and device-mapper-persistent-data…

Read More

Docker Tutorials: How to Install Docker in Ubuntu?

Install Docker Engine in Ubuntu NOTE – All commands you must run as root user or add a current user into a linux group name called “docker”…

Read More

Docker Lab, Excercise & Assignment – 7 – Docker Volume

Below is a very detailed tutorial and lab manual for learning Docker Volumes, using the Ubuntu image for practical, hands-on labs. This covers all major types of…

Read More

Docker Lab, Excercise & Assignment – 4 – Docker Networking

Hereโ€™s an in-depth, step-by-step tutorial and lab manual for Docker Networkingโ€”starting from basics, covering all core concepts, and providing a hands-on guide to every feature and command….

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x