In this
post, I am going to explain how to monitor a docker container using docker cli
and an user interface.
a. Monitor using Docker cli: docket
container stats
‘docker
container stats’ api used to get resource usage statistics of a container.
$docker container stats --help Usage: docker container stats [OPTIONS] [CONTAINER...] Display a live stream of container(s) resource usage statistics Options: -a, --all Show all containers (default shows just running) --format string Pretty-print images using a Go template --no-stream Disable streaming stats and only pull the first result --no-trunc Do not truncate output
Let’s run
wildfly container
$docker container run -d -p1234:8080 jboss/wildfly 8de8363baa48cd7f2d2406b4dae797b72abbac4c7112aa0f6e16e638822ae0f5
Execute
the command 'docker container ls', you can see that the container is up and
running.
$docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8de8363baa48 jboss/wildfly "/opt/jboss/wildfly/…" 41 seconds ago Up 40 seconds 0.0.0.0:1234->8080/tcp cranky_kowalevski
Now use
'docker container stats {container_id/container_name}' to see the container
statistics.
Example
docker
container stats 8de8363baa48
You can
see the statistics screen like below.
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 8de8363baa48 cranky_kowalevski 0.35% 309.3MiB / 1.952GiB 15.48% 1.04kB / 0B 328kB / 69.6kB 69
Press
CTRL+C to exit from the statistics screen.
How to extract specific column details
from statistics?
Using
format string you can extract specific column details.
For
example, Execute below command to get cpu percent usage of the container.
docker
container stats --format "{{.Container}} : {{.CPUPerc}}" 8de8363baa48
You can
even extract multiple columns information from the statistics.
For
example, below command extract both cpu and memory usgae information.
docker
container stats --format "{{.Container}} : {{.CPUPerc}}
{{.MemUsage}}" 8de8363baa48
Below
table summarize different place holders in format string.
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)
|
Get the statistics in table format
Below
snippet gets container statistics in table format.
docker
container stats --format "table {{.Name}} : \t{{.CPUPerc}}
\t{{.MemUsage}}" 8de8363baa48
NAME
: CPU % MEM USAGE / LIMIT
cranky_kowalevski
: 0.40% 310.6MiB / 1.952GiB
Print statistics without streaming
If you do
not want streaming of the statistics output, you can use --no-stream option.
$docker container stats --no-stream 8de8363baa48 CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 8de8363baa48 cranky_kowalevski 0.41% 312.2MiB / 1.952GiB 15.62% 1.39kB / 0B 328kB / 69.6kB 69
b. Getting container statistics using
curl command
You can
get the statistics of a container, by hitting below api.
'http://localhost/containers/{containerId
or containerName}/stats'
Example
$curl --unix-socket /var/run/docker.sock http://localhost/containers/8de8363baa48/stats {"read":"2019-04-26T05:54:29.8148362Z","preread":"0001-01-01T00:00:00Z","pids_stats":{"current":69},"blkio_stats":{"io_service_bytes_recursive":[{"major":8,"minor":0,"op":"Read","value":327680},{"major":8,"minor":0,"op":"Write","value":69632},{"major":8,"minor":0,"op":"Sync","value":0},{"major":8,"minor":0,"op":"Async","value":397312},{"major":8,"minor":0,"op":"Total","value":397312}],"io_serviced_recursive":[{"major":8,"minor":0,"op":"Read","value":15},{"major":8,"minor":0,"op":"Write","value":3},{"major":8,"minor":0,"op":"Sync","value":0},{"major":8,"minor":0,"op":"Async","value":18},{"major":8,"minor":0,"op":"Total","value":18}],"io_queue_recursive":[],"io_service_time_recursive":[],"io_wait_time_recursive":[],"io_merged_recursive":[],"io_time_recursive":[],"sectors_recursive":[]},"num_procs":0,"storage_stats":{},"cpu_stats":{"cpu_usage":{"total_usage":34034128328,"percpu_usage":[6629378428,4479924427,5938678226,6888436992,4704682180,5393028075],"usage_in_kernelmode":10310000000,"usage_in_usermode":18330000000},"system_cpu_usage":441807100000000,"online_cpus":6,"throttling_data":{"periods":0,"throttled_periods":0,"throttled_time":0}},"precpu_stats":{"cpu_usage":{"total_usage":0,"usage_in_kernelmode":0,"usage_in_usermode":0},"throttling_data":{"periods":0,"throttled_periods":0,"throttled_time":0}},"memory_stats":{"usage":328134656,"max_usage":333287424,"stats":{"active_anon":322752512,"active_file":208896,"cache":487424,"dirty":8192,"hierarchical_memory_limit":9223372036854771712,"hierarchical_memsw_limit":9223372036854771712,"inactive_anon":0,"inactive_file":278528,"mapped_file":32768,"pgfault":115607,"pgmajfault":14,"pgpgin":111944,"pgpgout":33028,"rss":322752512,"rss_huge":0,"total_active_anon":322752512,"total_active_file":208896,"total_cache":487424,"total_dirty":8192,"total_inactive_anon":0,"total_inactive_file":278528,"total_mapped_file":32768,"total_pgfault":115607,"total_pgmajfault":14,"total_pgpgin":111944,"total_pgpgout":33028,"total_rss":322752512,"total_rss_huge":0,"total_unevictable":0,"total_writeback":0,"unevictable":0,"writeback":0},"limit":2095575040},"name":"/cranky_kowalevski","id":"8de8363baa48cd7f2d2406b4dae797b72abbac4c7112aa0f6e16e638822ae0f5","networks":{"eth0":{"rx_bytes":1388,"rx_packets":18,"rx_errors":0,"rx_dropped":0,"tx_bytes":0,"tx_packets":0,"tx_errors":0,"tx_dropped":0}}}
No comments:
Post a Comment