Get docker run stats on exit
1.8K
This is a small utility designed to capture the statistics for the run of a Docker container before its destruction.
It is designed to be used as the main process of a Docker container that wraps the real command by waiting for it to exit and then querying the active Cgroup subsystems to gather their statistics. It dumps these statistics to a file as JSON and then exits with the exit code of the real command.
STATS_OUTPUT_FILE=/dev/stdout file path you want your output to be saved to (Default /dev/stdout)STATS_OUTPUT_PREFIX= any prefix you want to add before stats output (Default blank)Dockerfile
COPY --from=hasnat/docker-stats-on-exit-shim:exitgo2 /docker-stats-on-exit-shim .
ENTRYPOINT ["/docker-stats-on-exit-shim"]
CMD ["sleep", "1"]
Example Run
$ docker run --rm -ti hasnat/docker-stats-on-exit-shim:exitgo2 /bin/sleep 1
Output example
{"cgroup.controllers":"cpuset cpu io memory hugetlb pids rdma","cgroup.events.populated":"1","cgroup.events.frozen":"0","cgroup.freeze":"0","cgroup.kill":"","cgroup.max.depth":"max","cgroup.max.descendants":"max","cgroup.procs":"1","cgroup.procs":"67","cgroup.procs":"68","cgroup.stat.nr_descendants":"0","cgroup.stat.nr_dying_descendants":"0","cgroup.subtree_control":"","cgroup.threads":"1","cgroup.threads":"109","cgroup.threads":"110","cgroup.type":"domain","cpu.idle":"0","cpu.max.max":"100000","cpu.max.burst":"0","cpu.stat.usage_usec":"112557","cpu.stat.user_usec":"8732","cpu.stat.system_usec":"103824","cpu.stat.nr_periods":"0","cpu.stat.nr_throttled":"0","cpu.stat.throttled_usec":"0","cpu.stat.nr_bursts":"0","cpu.stat.burst_usec":"0","cpu.stat.local.throttled_usec":"0","cpu.weight":"100","cpu.weight.nice":"0","cpuset.cpus":"","cpuset.cpus.effective":"0-9","cpuset.cpus.partition":"member","cpuset.mems":"","cpuset.mems.effective":"0","hugetlb.1GB.current":"0","hugetlb.1GB.events.max":"0","hugetlb.1GB.events.local.max":"0","hugetlb.1GB.max":"max","hugetlb.1GB.numa_stat.total=0":"N0=0","hugetlb.1GB.rsvd.current":"0","hugetlb.1GB.rsvd.max":"max","hugetlb.2MB.current":"0","hugetlb.2MB.events.max":"0","hugetlb.2MB.events.local.max":"0","hugetlb.2MB.max":"max","hugetlb.2MB.numa_stat.total=0":"N0=0","hugetlb.2MB.rsvd.current":"0","hugetlb.2MB.rsvd.max":"max","hugetlb.32MB.current":"0","hugetlb.32MB.events.max":"0","hugetlb.32MB.events.local.max":"0","hugetlb.32MB.max":"max","hugetlb.32MB.numa_stat.total=0":"N0=0","hugetlb.32MB.rsvd.current":"0","hugetlb.32MB.rsvd.max":"max","hugetlb.64KB.current":"0","hugetlb.64KB.events.max":"0","hugetlb.64KB.events.local.max":"0","hugetlb.64KB.max":"max","hugetlb.64KB.numa_stat.total=0":"N0=0","hugetlb.64KB.rsvd.current":"0","hugetlb.64KB.rsvd.max":"max","io.stat":"254:0 rbytes=798720 wbytes=0 rios=50 wios=0 dbytes=0 dios=0","memory.current":"6676480","memory.events.low":"0","memory.events.high":"0","memory.events.max":"0","memory.events.oom":"0","memory.events.oom_kill":"0","memory.events.oom_group_kill":"0","memory.events.local.low":"0","memory.events.local.high":"0","memory.events.local.max":"0","memory.events.local.oom":"0","memory.events.local.oom_kill":"0","memory.events.local.oom_group_kill":"0","memory.high":"max","memory.low":"0","memory.max":"max","memory.min":"0","memory.oom.group":"0","memory.peak":"8974336","memory.reclaim":"","memory.stat.anon":"516096","memory.stat.file":"798720","memory.stat.kernel":"3772416","memory.stat.kernel_stack":"49152","memory.stat.pagetables":"114688","memory.stat.sec_pagetables":"0","memory.stat.percpu":"25440","memory.stat.sock":"0","memory.stat.vmalloc":"0","memory.stat.shmem":"0","memory.stat.zswap":"0","memory.stat.zswapped":"0","memory.stat.file_mapped":"0","memory.stat.file_dirty":"0","memory.stat.file_writeback":"0","memory.stat.swapcached":"0","memory.stat.anon_thp":"0","memory.stat.file_thp":"0","memory.stat.shmem_thp":"0","memory.stat.inactive_anon":"0","memory.stat.active_anon":"475136","memory.stat.inactive_file":"225280","memory.stat.active_file":"573440","memory.stat.unevictable":"0","memory.stat.slab_reclaimable":"251352","memory.stat.slab_unreclaimable":"603608","memory.stat.slab":"854960","memory.stat.workingset_refault_anon":"0","memory.stat.workingset_refault_file":"0","memory.stat.workingset_activate_anon":"0","memory.stat.workingset_activate_file":"0","memory.stat.workingset_restore_anon":"0","memory.stat.workingset_restore_file":"0","memory.stat.workingset_nodereclaim":"0","memory.stat.pgscan":"0","memory.stat.pgsteal":"0","memory.stat.pgscan_kswapd":"0","memory.stat.pgscan_direct":"0","memory.stat.pgscan_khugepaged":"0","memory.stat.pgsteal_kswapd":"0","memory.stat.pgsteal_direct":"0","memory.stat.pgsteal_khugepaged":"0","memory.stat.pgfault":"112536","memory.stat.pgmajfault":"7","memory.stat.pgrefill":"0","memory.stat.pgactivate":"0","memory.stat.pgdeactivate":"0","memory.stat.pglazyfree":"0","memory.stat.pglazyfreed":"0","memory.stat.zswpin":"0","memory.stat.zswpout":"0","memory.stat.thp_fault_alloc":"1","memory.stat.thp_collapse_alloc":"0","memory.swap.current":"0","memory.swap.events.high":"0","memory.swap.events.max":"0","memory.swap.events.fail":"0","memory.swap.high":"max","memory.swap.max":"max","memory.swap.peak":"0","memory.zswap.current":"0","memory.zswap.max":"max","pids.current":"3","pids.events.max":"0","pids.max":"max","pids.peak":"6","rdma.current":"","rdma.max":"","":""}
docker build -t docker-stats-on-exit-shim .
When developing, simply volume the source code into the container and run the shim.sh script
hasnat@MacBook-Pro docker-stats-on-exit-shim % docker run --rm -it -v `pwd`:`pwd` -w `pwd` --entrypoint=./shim.sh alpine 2>&1 | jq -c .
{"stat.cpu.0":129851,"stat.cpu.1":0,"stat.cpu.2":94753,"stat.cpu.3":91596921,"stat.cpu.4":2025,"stat.cpu.5":0,"stat.cpu.6":65631,"stat.cpu.7":0,"stat.cpu.8":0,"stat.cpu.9":0,"stat.cpu0.0":23172,"stat.cpu0.1":0,"stat.cpu0.2":15434,"stat.cpu0.3":9105882,"stat.cpu0.4":223,"stat.cpu0.5":0,"stat.cpu0.6":36760,"stat.cpu0.7":0,"stat.cpu0.8":0,"stat.cpu0.9":0,"stat.cpu1.0":24720,"stat.cpu1.1":0,"stat.cpu1.2":13194,"stat.cpu1.3":9131160,"stat.cpu1.4":272,"stat.cpu1.5":0,"stat.cpu1.6":20124,"stat.cpu1.7":0,"stat.cpu1.8":0,"stat.cpu1.9":0,"stat.cpu2.0":10137,"stat.cpu2.1":0,"stat.cpu2.2":8329,"stat.cpu2.3":9166215,"stat.cpu2.4":225,"stat.cpu2.5":0,"stat.cpu2.6":5305,"stat.cpu2.7":0,"stat.cpu2.8":0,"stat.cpu2.9":0,"stat.cpu3.0":10245,"stat.cpu3.1":0,"stat.cpu3.2":8184,"stat.cpu3.3":9170783,"stat.cpu3.4":197,"stat.cpu3.5":0,"stat.cpu3.6":1269,"stat.cpu3.7":0,"stat.cpu3.8":0,"stat.cpu3.9":0,"stat.cpu4.0":10181,"stat.cpu4.1":0,"stat.cpu4.2":8210,"stat.cpu4.3":9170805,"stat.cpu4.4":263,"stat.cpu4.5":0,"stat.cpu4.6":648,"stat.cpu4.7":0,"stat.cpu4.8":0,"stat.cpu4.9":0,"stat.cpu5.0":10160,"stat.cpu5.1":0,"stat.cpu5.2":8106,"stat.cpu5.3":9171248,"stat.cpu5.4":175,"stat.cpu5.5":0,"stat.cpu5.6":440,"stat.cpu5.7":0,"stat.cpu5.8":0,"stat.cpu5.9":0,"stat.cpu6.0":10232,"stat.cpu6.1":0,"stat.cpu6.2":8207,"stat.cpu6.3":9170922,"stat.cpu6.4":175,"stat.cpu6.5":0,"stat.cpu6.6":327,"stat.cpu6.7":0,"stat.cpu6.8":0,"stat.cpu6.9":0,"stat.cpu7.0":10172,"stat.cpu7.1":0,"stat.cpu7.2":8253,"stat.cpu7.3":9170417,"stat.cpu7.4":207,"stat.cpu7.5":0,"stat.cpu7.6":285,"stat.cpu7.7":0,"stat.cpu7.8":0,"stat.cpu7.9":0,"stat.cpu8.0":10401,"stat.cpu8.1":0,"stat.cpu8.2":8335,"stat.cpu8.3":9170437,"stat.cpu8.4":129,"stat.cpu8.5":0,"stat.cpu8.6":239,"stat.cpu8.7":0,"stat.cpu8.8":0,"stat.cpu8.9":0,"stat.cpu9.0":10426,"stat.cpu9.1":0,"stat.cpu9.2":8497,"stat.cpu9.3":9169045,"stat.cpu9.4":155,"stat.cpu9.5":0,"stat.cpu9.6":230,"stat.cpu9.7":0,"stat.cpu9.8":0,"stat.cpu9.9":0,"stat.intr.0":63415282,"stat.intr.1":0,"stat.intr.2":473129,"stat.intr.3":37451139,"stat.intr.4":0,"stat.intr.5":0,"stat.intr.6":0,"stat.intr.7":262600,"stat.intr.8":0,"stat.intr.9":0,"stat.intr.10":0,"stat.intr.11":0,"stat.intr.12":22836466,"stat.intr.13":0,"stat.intr.14":0,"stat.intr.15":1628628,"stat.intr.16":1730,"stat.intr.17":95009,"stat.intr.18":2656,"stat.intr.19":41800,"stat.intr.20":3,"stat.intr.21":3,"stat.intr.22":3,"stat.intr.23":3,"stat.intr.24":22,"stat.intr.25":620580,"stat.intr.26":1511,"stat.intr.27":0,"stat.intr.28":0,"stat.intr.29":0,"stat.intr.30":0,"stat.intr.31":0,"stat.intr.32":0,"stat.intr.33":0,"stat.intr.34":0,"stat.intr.35":0,"stat.intr.36":0,"stat.intr.37":0,"stat.intr.38":0,"stat.intr.39":0,"stat.intr.40":0,"stat.intr.41":0,"stat.intr.42":0,"stat.intr.43":0,"stat.intr.44":0,"stat.intr.45":0,"stat.intr.46":0,"stat.intr.47":0,"stat.intr.48":0,"stat.intr.49":0,"stat.intr.50":0,"stat.intr.51":0,"stat.intr.52":0,"stat.intr.53":0,"stat.intr.54":0,"stat.intr.55":0,"stat.intr.56":0,"stat.intr.57":0,"stat.intr.58":0,"stat.intr.59":0,"stat.intr.60":0,"stat.intr.61":0,"stat.intr.62":0,"stat.intr.63":0,"stat.intr.64":0,"stat.ctxt":94989015,"stat.btime":1747749569,"stat.processes":133604,"stat.procs_running":1,"stat.procs_blocked":0,"stat.softirq.0":40387904,"stat.softirq.1":0,"stat.softirq.2":6898530,"stat.softirq.3":1046,"stat.softirq.4":2492392,"stat.softirq.5":95243,"stat.softirq.6":0,"stat.softirq.7":397,"stat.softirq.8":18560397,"stat.softirq.9":0,"stat.softirq.10":12339899,"cgroup.controllers.cpuset.0":"cpu","cgroup.controllers.cpuset.1":"io","cgroup.controllers.cpuset.2":"memory","cgroup.controllers.cpuset.3":"hugetlb","cgroup.controllers.cpuset.4":"pids","cgroup.controllers.cpuset.5":"rdma","cgroup.events.populated":1,"cgroup.events.frozen":0,"cgroup.freeze":0,"cgroup.kill":"","cgroup.max.depth":"max","cgroup.max.descendants":"max","cgroup.pressure":1,"cgroup.procs":326,"cgroup.stat.nr_descendants":0,"cgroup.stat.nr_dying_descendants":0,"cgroup.subtree_control":"","cgroup.threads":394,"cgroup.type":"domain","cpu.idle":0,"cpu.max.max":100000,"cpu.max.burst":0,"cpu.pressure.some.avg10":"0.00","cpu.pressure.some.avg60":"0.00","cpu.pressure.some.avg300":"0.00","cpu.pressure.some.total":1887,"cpu.pressure.full.avg10":"0.00","cpu.pressure.full.avg60":"0.00","cpu.pressure.full.avg300":"0.00","cpu.pressure.full.total":1708,"cpu.stat.usage_usec":113862,"cpu.stat.user_usec":27859,"cpu.stat.system_usec":86002,"cpu.stat.nr_periods":0,"cpu.stat.nr_throttled":0,"cpu.stat.throttled_usec":0,"cpu.stat.nr_bursts":0,"cpu.stat.burst_usec":0,"cpu.stat.local.throttled_usec":0,"cpu.weight":100,"cpu.weight.nice":0,"cpuset.cpus":"","cpuset.cpus.effective":"0-9","cpuset.cpus.exclusive":"","cpuset.cpus.exclusive.effective":"","cpuset.cpus.partition":"member","cpuset.mems":"","cpuset.mems.effective":0,"hugetlb.1GB.current":0,"hugetlb.1GB.events.max":0,"hugetlb.1GB.events.local.max":0,"hugetlb.1GB.max":"max","hugetlb.1GB.numa_stat.total":0,"hugetlb.1GB.numa_stat.N0":0,"hugetlb.1GB.rsvd.current":0,"hugetlb.1GB.rsvd.max":"max","hugetlb.2MB.current":0,"hugetlb.2MB.events.max":0,"hugetlb.2MB.events.local.max":0,"hugetlb.2MB.max":"max","hugetlb.2MB.numa_stat.total":0,"hugetlb.2MB.numa_stat.N0":0,"hugetlb.2MB.rsvd.current":0,"hugetlb.2MB.rsvd.max":"max","hugetlb.32MB.current":0,"hugetlb.32MB.events.max":0,"hugetlb.32MB.events.local.max":0,"hugetlb.32MB.max":"max","hugetlb.32MB.numa_stat.total":0,"hugetlb.32MB.numa_stat.N0":0,"hugetlb.32MB.rsvd.current":0,"hugetlb.32MB.rsvd.max":"max","hugetlb.64KB.current":0,"hugetlb.64KB.events.max":0,"hugetlb.64KB.events.local.max":0,"hugetlb.64KB.max":"max","hugetlb.64KB.numa_stat.total":0,"hugetlb.64KB.numa_stat.N0":0,"hugetlb.64KB.rsvd.current":0,"hugetlb.64KB.rsvd.max":"max","io.max":"","io.pressure.some.avg10":"0.00","io.pressure.some.avg60":"0.00","io.pressure.some.avg300":"0.00","io.pressure.some.total":19,"io.pressure.full.avg10":"0.00","io.pressure.full.avg60":"0.00","io.pressure.full.avg300":"0.00","io.pressure.full.total":19,"io.stat":"","memory.current":7897088,"memory.events.low":0,"memory.events.high":0,"memory.events.max":0,"memory.events.oom":0,"memory.events.oom_kill":0,"memory.events.oom_group_kill":0,"memory.events.local.low":0,"memory.events.local.high":0,"memory.events.local.max":0,"memory.events.local.oom":0,"memory.events.local.oom_kill":0,"memory.events.local.oom_group_kill":0,"memory.high":"max","memory.low":0,"memory.max":"max","memory.min":0,"memory.oom.group":0,"memory.peak":12959744,"memory.pressure.some.avg10":"0.00","memory.pressure.some.avg60":"0.00","memory.pressure.some.avg300":"0.00","memory.pressure.some.total":0,"memory.pressure.full.avg10":"0.00","memory.pressure.full.avg60":"0.00","memory.pressure.full.avg300":"0.00","memory.pressure.full.total":0,"memory.reclaim":"","memory.stat.anon":212992,"memory.stat.file":4096,"memory.stat.kernel":10248192,"memory.stat.kernel_stack":49152,"memory.stat.pagetables":131072,"memory.stat.sec_pagetables":0,"memory.stat.percpu":26496,"memory.stat.sock":0,"memory.stat.vmalloc":0,"memory.stat.shmem":0,"memory.stat.zswap":0,"memory.stat.zswapped":0,"memory.stat.file_mapped":0,"memory.stat.file_dirty":0,"memory.stat.file_writeback":0,"memory.stat.swapcached":0,"memory.stat.anon_thp":0,"memory.stat.file_thp":0,"memory.stat.shmem_thp":0,"memory.stat.inactive_anon":0,"memory.stat.active_anon":139264,"memory.stat.inactive_file":4096,"memory.stat.active_file":0,"memory.stat.unevictable":0,"memory.stat.slab_reclaimable":270432,"memory.stat.slab_unreclaimable":1167072,"memory.stat.slab":1437504,"memory.stat.workingset_refault_anon":0,"memory.stat.workingset_refault_file":0,"memory.stat.workingset_activate_anon":0,"memory.stat.workingset_activate_file":0,"memory.stat.workingset_restore_anon":0,"memory.stat.workingset_restore_file":0,"memory.stat.workingset_nodereclaim":0,"memory.stat.pgscan":0,"memory.stat.pgsteal":0,"memory.stat.pgscan_kswapd":0,"memory.stat.pgscan_direct":0,"memory.stat.pgscan_khugepaged":0,"memory.stat.pgsteal_kswapd":0,"memory.stat.pgsteal_direct":0,"memory.stat.pgsteal_khugepaged":0,"memory.stat.pgfault":83511,"memory.stat.pgmajfault":0,"memory.stat.pgrefill":0,"memory.stat.pgactivate":0,"memory.stat.pgdeactivate":0,"memory.stat.pglazyfree":0,"memory.stat.pglazyfreed":0,"memory.stat.zswpin":0,"memory.stat.zswpout":0,"memory.stat.zswpwb":0,"memory.stat.thp_fault_alloc":1,"memory.stat.thp_collapse_alloc":0,"memory.stat.thp_swpout":0,"memory.stat.thp_swpout_fallback":0,"memory.swap.current":0,"memory.swap.events.high":0,"memory.swap.events.max":0,"memory.swap.events.fail":0,"memory.swap.high":"max","memory.swap.max":"max","memory.swap.peak":0,"memory.zswap.current":0,"memory.zswap.max":"max","memory.zswap.writeback":1,"pids.current":3,"pids.events.max":0,"pids.max":"max","pids.peak":6,"rdma.current":"","rdma.max":"","":""}
cat command to get all/any stats. I will update the code once I read the docs.Content type
Image
Digest
Size
4.6 MB
Last updated
over 7 years ago
docker pull hasnat/docker-stats-on-exit-shim