How to use docker container stats command to monitor container resources

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively monitor the resource usage of your Docker containers using the docker stats command. You will explore how to display live resource usage for all running containers, as well as for specific containers by name or ID.

Furthermore, you will discover how to view resource usage for all containers, including stopped ones, and how to customize the output format of the docker stats command using a custom template. Finally, you will learn how to obtain a single snapshot of container statistics without continuous streaming.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/stop("Stop Container") docker/ContainerOperationsGroup -.-> docker/top("Display Running Processes in Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") subgraph Lab Skills docker/ps -.-> lab-555123{{"How to use docker container stats command to monitor container resources"}} docker/stop -.-> lab-555123{{"How to use docker container stats command to monitor container resources"}} docker/top -.-> lab-555123{{"How to use docker container stats command to monitor container resources"}} docker/pull -.-> lab-555123{{"How to use docker container stats command to monitor container resources"}} end

Display live resource usage for all running containers

In this step, you will learn how to display the live resource usage for all running Docker containers using the docker stats command. This command provides a real-time stream of container resource usage data, including CPU, memory, network I/O, and block I/O.

First, let's start a few containers so we have something to monitor. We will use the ubuntu image and run them in detached mode (-d) so they run in the background. We will also give them names for easier identification.

docker pull ubuntu
docker run -d --name container1 ubuntu sleep infinity
docker run -d --name container2 ubuntu sleep infinity
docker run -d --name container3 ubuntu sleep infinity

Now that we have some containers running, we can use the docker stats command to see their resource usage.

docker stats

You will see a table with information about each running container, including its ID, name, CPU usage, memory usage, network I/O, block I/O, and PIDs. This output updates in real-time.

To stop the docker stats command, press Ctrl+C.

Display live resource usage for specific containers by name or ID

In the previous step, you learned how to display the resource usage for all running containers. In this step, you will learn how to display the resource usage for specific containers by providing their names or IDs to the docker stats command.

To display the stats for a single container, simply provide its name or ID after the docker stats command. For example, to see the stats for container1:

docker stats container1

You will see the real-time resource usage for only container1. Press Ctrl+C to stop the output.

You can also specify multiple container names or IDs to see the stats for several containers at once. For example, to see the stats for container1 and container2:

docker stats container1 container2

This will display the real-time resource usage for both container1 and container2. Press Ctrl+C to stop the output.

Using container names is generally easier to remember and work with than container IDs, especially when you have multiple containers running.

Display resource usage for all containers (running and stopped)

By default, docker stats only shows statistics for running containers. In this step, you will learn how to display resource usage for all containers, including those that are stopped, using the -a or --all flag.

First, let's stop one of the containers we started in the previous steps.

docker stop container3

Now, if you run docker stats without any flags, you will only see container1 and container2 as they are still running.

docker stats

Press Ctrl+C to stop the output.

To see the stats for all containers, including the stopped container3, use the -a flag:

docker stats -a

You will now see container1, container2, and container3 listed. For stopped containers, most of the resource usage fields will show 0% or 0B, as they are not actively consuming resources.

This is useful for getting a complete overview of all containers on your system and their last known resource usage before they were stopped.

Format the output of container stats using a custom template

In this step, you will learn how to format the output of the docker stats command using a custom Go template. This allows you to display only the information you need and in a format that is easy to parse or read.

The docker stats command supports the --format flag, which takes a Go template string as an argument. You can use various placeholders within the template to represent different pieces of container information. Some common placeholders include:

  • .ContainerID: The container's ID.
  • .Name: The container's name.
  • .CPUPerc: The CPU usage percentage.
  • .MemUsage: The memory usage.
  • .NetIO: The network I/O.
  • .BlockIO: The block I/O.
  • .PIDs: The number of PIDs.

Let's try formatting the output to show only the container name, CPU usage, and memory usage.

docker stats --format "{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"

In this command:

  • --format specifies that we want to use a custom format.
  • "{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}" is the Go template string.
    • {{.Name}} displays the container name.
    • \t inserts a tab character for separation.
    • {{.CPUPerc}} displays the CPU usage percentage.
    • {{.MemUsage}} displays the memory usage.

You will see a live stream of the container name, CPU usage, and memory usage, separated by tabs. Press Ctrl+C to stop the output.

You can experiment with different placeholders and formatting to get the output you need. For example, to include the container ID and network I/O:

docker stats --format "ID: {{.ContainerID}}\tName: {{.Name}}\tNetIO: {{.NetIO}}"

This provides a more customized view of the container statistics.

Get a single snapshot of container stats without streaming

By default, docker stats provides a live stream of resource usage. However, sometimes you might only need a single snapshot of the current stats without the continuous updates. In this step, you will learn how to achieve this using the --no-stream flag.

The --no-stream flag tells docker stats to collect the statistics once and then exit, rather than continuously updating the output.

Let's get a single snapshot of the stats for all running containers:

docker stats --no-stream

You will see the table of container stats appear once, and then the command will exit. This is useful for scripting or when you just need a quick look at the current resource usage.

You can combine --no-stream with other flags, such as -a to see a snapshot of all containers (running and stopped):

docker stats -a --no-stream

This will display a single snapshot of all containers.

You can also combine it with specifying specific containers:

docker stats container1 container2 --no-stream

This will give you a single snapshot of the stats for container1 and container2.

Using --no-stream is efficient when you don't need the real-time updates and just want to capture the current state of resource usage.

Summary

In this lab, you learned how to effectively use the docker stats command to monitor the resource usage of your Docker containers. You began by displaying the live resource usage for all running containers, observing real-time data streams for CPU, memory, network I/O, and block I/O.

Subsequently, you refined your monitoring by focusing on specific containers, using their names or IDs to view their individual resource consumption. This allowed for targeted analysis of container performance.