Created
March 1, 2017 03:30
-
-
Save hisplan/0b2c13ac78f61b8868482b9cfe6d9e65 to your computer and use it in GitHub Desktop.
Get Docker Size in MiB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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