Get container stats based on resource usage Run in API Explorer
This endpoint returns a live stream of a container’s resource usage statistics.
The precpu_stats
is the CPU statistic of the previous read, and is
used to calculate the CPU usage percentage. It is not an exact copy
of the cpu_stats
field.
If either precpu_stats.online_cpus
or cpu_stats.online_cpus
is
nil then for compatibility with older daemons the length of the
corresponding cpu_usage.percpu_usage
array should be used.
On a cgroup v2 host, the following fields are not set
blkio_stats
: all fields other thanio_service_bytes_recursive
cpu_stats
:cpu_usage.percpu_usage
memory_stats
:max_usage
andfailcnt
Also,memory_stats.stats
fields are incompatible with cgroup v1.
To calculate the values shown by the stats
command of the docker cli tool
the following formulas can be used:
- used_memory =
memory_stats.usage - memory_stats.stats.cache
- available_memory =
memory_stats.limit
- Memory usage % =
(used_memory / available_memory) * 100.0
- cpu_delta =
cpu_stats.cpu_usage.total_usage - precpu_stats.cpu_usage.total_usage
- system_cpu_delta =
cpu_stats.system_cpu_usage - precpu_stats.system_cpu_usage
- number_cpus =
length(cpu_stats.cpu_usage.percpu_usage)
orcpu_stats.online_cpus
- CPU usage % =
(cpu_delta / system_cpu_delta) * number_cpus * 100.0
GET
/containers/{id}/stats
curl \
--request GET 'http://api.example.com/v1.49/containers/{id}/stats'
Response examples (200)
{
"name": "boring_wozniak",
"id": "ede54ee1afda366ab42f824e8a5ffd195155d853ceaec74a927f249ea270c743",
"read": "2025-01-16T13:55:22.165243637Z",
"preread": "2025-01-16T13:55:21.160452595Z",
"pids_stats": {
"current": 5,
"limit": 18446744073709551615
},
"blkio_stats": {
"io_time_recursive": null,
"sectors_recursive": null,
"io_queue_recursive": null,
"io_merged_recursive": null,
"io_serviced_recursive": null,
"io_wait_time_recursive": null,
"io_service_time_recursive": null,
"io_service_bytes_recursive": [
{
"op": "read",
"major": 254,
"minor": 0,
"value": 7593984
},
{
"op": "write",
"major": 254,
"minor": 0,
"value": 100
}
]
},
"num_procs": 16,
"storage_stats": {
"read_count_normalized": 7593984,
"read_size_bytes": 7593984,
"write_count_normalized": 7593984,
"write_size_bytes": 7593984
},
"cpu_stats": {
"cpu_usage": {
"total_usage": 29912000,
"percpu_usage": [
29912000
],
"usage_in_kernelmode": 21994000,
"usage_in_usermode": 7918000
},
"system_cpu_usage": 5,
"online_cpus": 5,
"throttling_data": {
"periods": 0,
"throttled_periods": 0,
"throttled_time": 0
}
},
"precpu_stats": {
"cpu_usage": {
"total_usage": 29912000,
"percpu_usage": [
29912000
],
"usage_in_kernelmode": 21994000,
"usage_in_usermode": 7918000
},
"system_cpu_usage": 5,
"online_cpus": 5,
"throttling_data": {
"periods": 0,
"throttled_periods": 0,
"throttled_time": 0
}
},
"memory_stats": {
"usage": 0,
"max_usage": 0,
"stats": {
"anon": 1572864,
"file": 7626752,
"slab": 1180928,
"sock": 0,
"shmem": 0,
"pgscan": 0,
"pgfault": 2042,
"pgsteal": 0,
"anon_thp": 0,
"pgrefill": 0,
"file_dirty": 0,
"pgactivate": 0,
"pglazyfree": 0,
"pgmajfault": 45,
"active_anon": 1572864,
"active_file": 5115904,
"file_mapped": 2723840,
"pglazyfreed": 0,
"unevictable": 0,
"kernel_stack": 16384,
"pgdeactivate": 0,
"inactive_anon": 0,
"inactive_file": 2510848,
"file_writeback": 0,
"thp_fault_alloc": 1,
"slab_reclaimable": 725576,
"slab_unreclaimable": 455352,
"thp_collapse_alloc": 0,
"workingset_refault": 0,
"workingset_activate": 0,
"workingset_nodereclaim": 0
},
"failcnt": 0,
"limit": 8217579520,
"commitbytes": 0,
"commitpeakbytes": 0,
"privateworkingset": 0
},
"networks": {
"eth0": {
"rx_bytes": 5338,
"tx_bytes": 648,
"rx_errors": 0,
"tx_errors": 0,
"rx_dropped": 0,
"rx_packets": 36,
"tx_dropped": 0,
"tx_packets": 8
},
"eth5": {
"rx_bytes": 4641,
"tx_bytes": 690,
"rx_errors": 0,
"tx_errors": 0,
"rx_dropped": 0,
"rx_packets": 26,
"tx_dropped": 0,
"tx_packets": 9
}
}
}
Response examples (404)
{
"message": "No such container: c2ada9df5af8"
}
Response examples (404)
{
"message": "Something went wrong."
}
Response examples (500)
{
"message": "Something went wrong."
}
Response examples (500)
{
"message": "Something went wrong."
}