CodeGym /Courses /Docker SELF /Built-in Monitoring Tools

Built-in Monitoring Tools

Docker SELF
Level 21 , Lesson 2
Available

3.1 The docker stats Command

Docker gives you a handy built-in tool for real-time container monitoring — the docker stats command. This command lets you track key performance metrics for your containers, like CPU, memory, network, and disk usage. Let’s dive into how to use docker stats to get insights into your containers' status and performance.

The docker stats command provides dynamic stats on container performance in real time. It shows the following metrics for each container:

  • CPU %: the percentage of CPU usage by the container.
  • MEM USAGE / LIMIT: the memory usage by the container and its set limit.
  • MEM %: the percentage of memory usage by the container.
  • NET I/O: network traffic (incoming and outgoing).
  • BLOCK I/O: the amount of disk input/output operations.
  • PIDS: the number of processes running inside the container.

Basic Usage of the docker stats Command

Running the docker stats command without parameters displays metrics for all running containers:

Terminal


docker stats 

Example output for the docker stats command:

Terminal


CONTAINER ID   NAME                CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O         PIDS
d9b100f2f636   my_nginx            0.07%     1.24MiB / 1.952GiB    0.06%     1.68kB / 0B       0B / 12.3MB       2
fa3f3f3f3f3f   my_postgres         2.13%     230.5MiB / 1.952GiB   11.52%    2.12MB / 2.05MB   85.6MB / 45.2MB   10

Monitoring a Specific Container

You can track metrics for a specific container by specifying its name or ID:

Terminal


docker stats my_nginx 

3.2 Settings for the docker stats Command

Options for the docker stats Command

The docker stats command supports several options to customize the output:

  • --all or -a: display metrics for all containers, including stopped ones.
  • --no-stream: display metrics once and then exit the command.
  • --format: customize output using templates.

Example of Using the --no-stream Option

Displaying metrics once for all containers:

Terminal

docker stats --no-stream

Example of Using the --format Option

Formatted output:

Terminal

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

Example of output:

Terminal

CONTAINER ID   CPU %     MEM USAGE / LIMIT
d9b100f2f636   0.07%     1.24MiB / 1.952GiB
fa3f3f3f3f3f   2.13%     230.5MiB / 1.952GiB

3.3 Practical Use of docker stats

Example 1: Tracking Server Load

You can use docker stats to monitor containers on the server and identify containers with high CPU or memory usage. This helps you optimize resource usage and improve app performance.

Terminal

docker stats

Example 2: Monitoring a Specific Container During Testing

When testing new app versions or changing configurations, you can monitor specific container metrics to ensure there's no negative impact on performance.

Terminal

docker stats my_test_container

Example 3: Integration with Monitoring Systems

You can integrate the output of docker stats with external monitoring systems using the --format option to create an output format compatible with your monitoring tool.

Terminal

docker stats --no-stream --format "{{.Container}}: CPU {{.CPUPerc}}, MEM {{.MemUsage}}"

Limitations of the docker stats Command

Although docker stats provides useful real-time metrics, it has some limitations:

  • Limited Metrics: The command only displays basic metrics and doesn't provide detailed container state information.
  • Manual Usage: docker stats is convenient for manual use, but for automated monitoring and alerting, it's better to use specialized tools like Prometheus and Grafana.
3
Task
Docker SELF, level 21, lesson 2
Locked
Using the docker stats command to monitor all containers
Using the docker stats command to monitor all containers
3
Task
Docker SELF, level 21, lesson 2
Locked
Formatted output of metrics using docker stats
Formatted output of metrics using docker stats
3
Task
Docker SELF, level 21, lesson 2
Locked
Monitoring a specific container using docker stats
Monitoring a specific container using docker stats
3
Task
Docker SELF, level 21, lesson 2
Locked
Monitoring with one-time metrics output
Monitoring with one-time metrics output
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION