How to use the command 'docker stats' (with examples)

How to use the command 'docker stats' (with examples)

The ‘docker stats’ command allows users to display a live stream of resource usage statistics for containers. This can be useful for monitoring the performance and resource consumption of running containers.

Use case 1: Display a live stream for the statistics of all running containers

Code:

docker stats

Motivation: The ‘docker stats’ command without any arguments will display a live stream of the statistics for all running containers on the Docker host. This can be helpful for quickly monitoring the resource usage of multiple containers at once.

Explanation: Running the ‘docker stats’ command without any arguments will display a live stream of the resource usage statistics for all running containers. This includes information such as the container ID, container name, CPU usage percentage, memory usage, network I/O, and disk I/O.

Example output:

CONTAINER ID   NAME               CPU %   MEM USAGE / LIMIT     MEM %   NET I/O     BLOCK I/O   PIDS
c3b4e4a1f6c1   container1         0.67%   4.942MiB / 1.952GiB   0.25%   2.53kB/s    0B/s        22
4a08a6b8e3a3   container2         3.42%   280.3MiB / 1.952GiB   14.03%  5.38kB/s    0B/s        50

Use case 2: Display a live stream of statistics for a space-separated list of containers

Code:

docker stats container_name

Motivation: The ‘docker stats’ command can also be used to display a live stream of statistics for a specific container or a space-separated list of containers. This can be useful for monitoring specific containers that are of particular interest.

Explanation: By providing the name(s) of the container(s) as arguments, separated by spaces, the ‘docker stats’ command will display a live stream of the resource usage statistics for the specified container(s). This can be used to monitor the resource consumption of specific containers during testing or troubleshooting.

Example output:

CONTAINER ID   NAME               CPU %   MEM USAGE / LIMIT     MEM %   NET I/O     BLOCK I/O   PIDS
c3b4e4a1f6c1   container1         0.67%   4.942MiB / 1.952GiB   0.25%   2.53kB/s    0B/s        22

Use case 3: Change the columns format to display container’s CPU usage percentage

Code:

docker stats --format ".Name:\t.CPUPerc"

Motivation: The ‘docker stats’ command allows users to customize the format of the displayed statistics. By changing the columns format, users can focus on specific information they need, such as the CPU usage percentage of the containers.

Explanation: The ‘–format’ flag followed by the desired format specifier allows users to change the columns format of the ‘docker stats’ command output. In this example, the format “.Name:\t.CPUPerc” is used, which displays the container name followed by the CPU usage percentage.

Example output:

CONTAINER ID   NAME       CPU %
c3b4e4a1f6c1   container1 0.67%
4a08a6b8e3a3   container2 3.42%

Use case 4: Display statistics for all containers (both running and stopped)

Code:

docker stats --all

Motivation: By default, the ‘docker stats’ command only displays statistics for running containers. However, there may be cases where users need to view statistics for both running and stopped containers. This can be useful for historical analysis or troubleshooting.

Explanation: Adding the ‘–all’ flag to the ‘docker stats’ command instructs it to display statistics for all containers, including both running and stopped containers. This can provide a comprehensive overview of resource usage across all containers on the Docker host.

Example output:

CONTAINER ID   NAME             CPU %   MEM USAGE / LIMIT     MEM %   NET I/O     BLOCK I/O   PIDS
c3b4e4a1f6c1   container1       0.67%   4.942MiB / 1.952GiB   0.25%   2.53kB/s    0B/s        22
4a08a6b8e3a3   container2       3.42%   280.3MiB / 1.952GiB   14.03%  5.38kB/s    0B/s        50
987994b35e0f   container_stopped 0.00%   0B / 0B               0.00%   0B/s        0B/s        0

Use case 5: Disable streaming stats and only pull the current stats

Code:

docker stats --no-stream

Motivation: By default, the ‘docker stats’ command continuously streams the live statistics of containers. However, there may be cases where users only need to retrieve the current statistics once and not continuously monitor them.

Explanation: The ‘–no-stream’ flag is used to disable the streaming of statistics and only pull the current statistics once. This can be useful when users need to fetch the current resource usage for containers without the continuous real-time updates.

Example output:

CONTAINER ID   NAME               CPU %   MEM USAGE / LIMIT     MEM %   NET I/O     BLOCK I/O   PIDS
c3b4e4a1f6c1   container1         0.67%   4.942MiB / 1.952GiB   0.25%   2.53kB/s    0B/s        22

Conclusion:

The ‘docker stats’ command is a powerful tool for monitoring the resource usage of containers. By utilizing the various command options, users can customize the output format, monitor specific containers, view statistics for both running and stopped containers, and choose between real-time streaming or fetching the current statistics once. This level of flexibility makes the ‘docker stats’ command a valuable asset in managing Docker container deployments.

Related Posts

How to use the command mplayer (with examples)

How to use the command mplayer (with examples)

MPlayer is a cross-platform multimedia player that can play both local media files and stream content from URLs.

Read More
How to use the command pw-link (with examples)

How to use the command pw-link (with examples)

The pw-link command is used to manage links between ports in PipeWire, which is a server for handling audio and video streams.

Read More
How to use the command "pio" (with examples)

How to use the command "pio" (with examples)

The “pio” command is a development environment for embedded boards. It provides a range of subcommands that allow users to build, upload, and manage projects for various embedded platforms.

Read More