Skip to content

Instantly share code, notes, and snippets.

@hisplan
Created March 1, 2017 03:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hisplan/0b2c13ac78f61b8868482b9cfe6d9e65 to your computer and use it in GitHub Desktop.
Save hisplan/0b2c13ac78f61b8868482b9cfe6d9e65 to your computer and use it in GitHub Desktop.
Get Docker Size in MiB
#!/bin/bash
function convert_to_mib {
local mib=$1
if [ $2 = "KB" ]
then
# if less than 1 MiB, just return 1 MiB
mib=1
fi
if [ $2 = "GB" ]
then
# if GiB, convert to MiB
mib=`echo "$1 * 1000" | bc -l`
fi
echo $mib
}
function get_docker_size_in_mib {
# get docker image size for a given name $1
# if there are more than two images found for a given name, we will use the first appearing one
# returned string would look like '3.98 MB'
local size_string=`sudo docker images $1 --format "{{.Size}}" | head -1`
# split at the space char, take the numeric portion, add 5, and round up
# fixme: round up done using python script
local size=`echo ${size_string} | awk -F' ' '{ print $1 }' | python -c "print int(round(float(raw_input()) + 5))"`
# split at the space char, take the unit portion (e.g. B, KB, MB, GB)
local size_unit=`echo ${size_string} | awk -F' ' '{ print $2 }'`
# express in MiB
size=$(convert_to_mib ${size} ${size_unit})
echo ${size}
}
if [ -s "Singularity" ]
then
echo "exists"
fi
size=$(get_docker_size_in_mib $1)
echo $size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment