Skip to content

Instantly share code, notes, and snippets.

@dmcgowan
Last active May 17, 2016 22:05
Show Gist options
  • Save dmcgowan/57e56d8fe814df87fbdcd8f4709cb9b0 to your computer and use it in GitHub Desktop.
Save dmcgowan/57e56d8fe814df87fbdcd8f4709cb9b0 to your computer and use it in GitHub Desktop.
Docker image disk usage tool
#!/bin/env sh
# imagesize.sh calculates the on disk size of images by pulling
# and comparing inode and block usage. Requires using a fresh
# device to accurately measure the difference caused by each
# image. This is intended to measure the on disk size caused
# by pulling an image with docker rather than the sum of all
# the file usage just inside the docker graph driver directory.
#
# Usage:
# sh imagesize.sh <device> <storage driver> [<image>, ...]
#
# How to use:
# $ fallocate -l 18GB testvolume
# $ device=$(sudo losetup -f --show ./testvolume)
# $ sudo mkfs.ext4 -F $device
# $ sudo sh imagesize.sh $device overlay ubuntu:latest golang:latest
tmpdir=$(mktemp -d)
device=$1
graphdriver=$2
shift
shift
mount $device $tmpdir
echo "Mounted $device to $tmpdir"
echo "Starting docker engine using $graphdriver storage driver"
docker daemon -D -s $graphdriver -g $tmpdir > /tmp/dockertestlog.txt 2>&1 &
sleep 10
base=$(df --output=used $tmpdir | tail -n 1)
basei=$(df --output=iused $tmpdir | tail -n 1)
function usage_diff() {
usage=$(df --output=used $tmpdir | tail -n 1)
usagei=$(df --output=iused $tmpdir | tail -n 1)
diff=$(expr $usage - $base)
diffi=$(expr $usagei - $basei)
base=$usage
basei=$usagei
echo "Size($1): $diff"
echo "Inodes($1): $diffi"
}
for image in "$@"; do
echo "Pulling $image"
docker pull $image
usage_diff $image
done
kill $(cat /var/run/docker.pid)
sleep 2
umount $tmpdir
rmdir $tmpdir
echo "Unmounted and remove $tmpdir"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment