how to get docker stats using shell script

#!/bin/bash

# This script is used to complete the output of the docker stats command.
# The docker stats command does not compute the total amount of resources (RAM or CPU)

# Get the total amount of RAM, assumes there are at least 1024*1024 KiB, therefore > 1 GiB
HOST_MEM_TOTAL=$(grep MemTotal /proc/meminfo | awk '{print $2/1024/1024}')

# Get the output of the docker stat command. Will be displayed at the end
# Without modifying the special variable IFS the ouput of the docker stats command won't have
# the new lines thus resulting in a failure when using awk to process each line
IFS=;
DOCKER_STATS_CMD=`docker stats --no-stream --format "table {{.MemPerc}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.Name}}"`

SUM_RAM=`echo $DOCKER_STATS_CMD | tail -n +2 | sed "s/%//g" | awk '{s+=$1} END {print s}'`
SUM_CPU=`echo $DOCKER_STATS_CMD | tail -n +2 | sed "s/%//g" | awk '{s+=$2} END {print s}'`
SUM_RAM_QUANTITY=`LC_NUMERIC=C printf %.2f $(echo "$SUM_RAM*$HOST_MEM_TOTAL*0.01" | bc)`

# Output the result
echo $DOCKER_STATS_CMD
echo -e "${SUM_RAM}%\t\t\t${SUM_CPU}%\t\t${SUM_RAM_QUANTITY}GiB / ${HOST_MEM_TOTAL}GiB\tTOTAL"

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
read docker stats in bash script command to print docker container resource docker swarm memory usage get docker memory usage docker system status activity linux see docker cpu usage per container docker container host config stats how to know resiurces consudemend by docker desktoo docker show memory allocated docker show memory usage per container docker service stats see docker nodes cpu usage see docker swarm cpu usage how to check memory usage of docker container docker show ram of each containers docker show memory of each containers how to get system usage stats of a particular docker contaienr docker stats show container name docker image ram usage get docker container memory usage check how much resources docker uses docker see ram used docker inspect stats docker stats cpu usage 0% docker stats cpu 0% check how much memory docker container uses docker cpu monitoring docker block usage of docker.io docker instance memory usage docker check ram usage docker-compose stat monitor docker container memory usage docker cpu and memory usage check memory dedicted to docker docker pids docker ps show resource usage docker per container memoery usgage run shell script in docker image docker windows memory usage docker monitor memory usage command docker monitor memory usage how to stat docker docker command run shell script docker get processor serial number ram using docker show docker machine check memory check docker instance resource usage docker monitor docker compose cpu thread for each service docker memory usage inside container test docker container memory docker cpu usage docker cpu uspage NET I/O docker limit docker network usage how to get docker stats check docker usage docker get container stats inside the container docker stats reference web interface cpu usage linux docker resources usage docker check docker container net limit docker compose ram monitor docker memory consume docker check container resource utilization docker heavy resource usage docker check memory usage of indiviul container docker find memory usage redatam statistics on docker how to check actual docker container memory from inside how to check actual docker container memory docker activity monitor docker container stats all containers Docker.DotNet container "stats" Docker.DotNet container "stats docker block io Docker.DotNet container stats dotnet.docker container stats docker container cpu usage docker see how much memory a container is using docker check memory utilization docker resource usage understanding docker stats docker stat alpine memory docker stat alphine memory get docker stats of container docker stats interval container stats docker compose live container stats docker compose how much memory a docker instance is using docker check memory usage how to monitor cpu and memory docker container docker top for all containers resouce consumption for docker docker show most cpu docker container top memory check docker container allocated memory docker show memory usage monitor docker memory usage by each container docker ps memory view docker memory in ubuntu docker ram usage docker inspect memory usage docker see memory usage docker-compose check memory usage how to check memory consumed by all docker container/images docker stats real time how to check docker ram memory how to see containers usages docker watch memory usage check docker container cpu usage docker inspect memory check cpu usage of docker container docker host stats docker shell get memory usage docker container memory usage show memory usage per container docker container see cpu usage docker stats command check current memory usage inside container docker windows see usage ram and cpu docker windows see usage docker show cpu usage view docker containers using the most cpu docker system resource useage windows docker memory usage docker total memory usage docker container net i/o out of memory docker container NET I/O memory limit docker container NET I/O limit docker stats docker see ram view details docker container cpu usage memory usage docker from inside container memory usage docker container how to check docker ram and cpu allocation check docker container usage
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source