Docker container monitoring using «Docker stats»
Along with orchestration and security, monitoring is another major challenge faced by organizations that have adopted containerization/Docker technologies. This is very critical in production environments, where you need to closely monitor how your microservices are performing and whats happening inside the Docker containers. In this article we will explain how to monitor docker containers using “docker stats” .
Docker Stats
The “docker stats” command allows you to live stream a container’s runtime metrics. The metrics include CPU usage, Memory usage, Network IO, Disk IO .
“docker stats” command returns data stream for running containers. To limit data to one or more specific containers, specify a list of container names or ids separated by a space. You can specify a stopped container but stopped containers do not return any data.
DESCRIPTION
1 2 3 4 5 6 7 8 9 10 | Display a live stream of container(s) resource usage statistics Usage docker stats [OPTIONS] [CONTAINER...] Options Name, shorthand Default Description —all, –a false Show all containers (default shows just running) —format Pretty–print images using a Go template —no–stream false Disable streaming stats and only pull the first result |
Sample Usage of Docker stats
1 2 3 4 5 6 7 8 9 10 11 | test.test.com:~ test$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 33269a610a4b centos:7 “/bin/bash” 3 months ago Up 2 minutes grafana test.test.com:~ test$ test.test.com:~ test$ docker stats CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 33269a610a4b 0.03% 6.316 MiB / 1.952 GiB 0.32% 0 B / 0 B 5.78 MB / 0 B 1 CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 33269a610a4b 0.00% 6.316 MiB / 1.952 GiB 0.32% 0 B / 0 B 5.78 MB / 0 B 1 |
Formatting “docker stats” output
Docker stats has “–format” option, which enabled us to define the way metrics printed to console. We will define templates in Go format and Valid placeholders for the Go template are listed below:
Valid placeholders for the Go template are
Placeholder | Description |
---|---|
.Container |
Container name or ID (user input) |
.Name |
Container name |
.ID |
Container ID |
.CPUPerc |
CPU percentage |
.MemUsage |
Memory usage |
.NetIO |
Network IO |
.BlockIO |
Block IO |
.MemPerc |
Memory percentage (Not available on Windows) |
.PIDs |
Number of PIDs (Not available on Windows) |
Sample Inputs and Outputs using Templates
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | test.test.com:~ test$ docker stats —format “{{.Container}}: {{.CPUPerc}}” 33269a610a4b: 0.01% 33269a610a4b: 0.00% 33269a610a4b: 0.00% 33269a610a4b: 0.00% 33269a610a4b: 0.00% 33269a610a4b: 0.00% 33269a610a4b: 0.00% 33269a610a4b: 0.00% test.test.com:~ test$ docker stats —format “table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}” NAME CPU % MEM USAGE / LIMIT grafana 0.01% 6.316 MiB / 1.952 GiB NAME CPU % MEM USAGE / LIMIT grafana 0.00% 6.316 MiB / 1.952 GiB |
SUMMARY
In this article we have explained how to monitor docker container CPU, memory and disk IO metrics using “docker stats” . Please find more detailed explanation at official website here