Created
March 11, 2020 03:39
-
-
Save torson/a58a01f3faf2e0f7bf98b0e5f6ccffc2 to your computer and use it in GitHub Desktop.
Docker multiple images unique layers size total sum
This file contains 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
#!/usr/bin/perl | |
## Purpose: | |
# To get the real size of your app that is comprised of many services/images. | |
# This script sums only the sizes of unique image layers. | |
# This way you can get a better sense of the total size of your app and make optimizations if needed. | |
## Usage: | |
# ./docker_images_size.pl IMAGE1:TAG1 IMAGE2:TAG2 ... | |
## Example: | |
# ./docker_images_size.pl eventstore/eventstore:release-5.0.3 eventstore/eventstore:release-5.0.6 | |
use strict; | |
use warnings; | |
my $cmd; | |
my $cmdOut; | |
my @lines; | |
my %hImageSizes; | |
my $totalSize=0; | |
my $prevImageId = ''; | |
my @argvImages = (); | |
for (my $iargv=0; $iargv<=$#ARGV; $iargv++) { | |
push(@argvImages, $ARGV[$iargv]); | |
} | |
foreach my $imageName (@argvImages) { | |
print "-> calculating size for image: $imageName\n"; | |
$cmd = "docker image history --format \"{{.ID}} {{.Size}}\" $imageName"; | |
# print "$cmd\n"; | |
$cmdOut = `$cmd`; | |
# print "$cmdOut\n"; | |
@lines = split(/\n/, $cmdOut); | |
foreach my $line (@lines) { | |
my @aLine = split(' ', $line); | |
my $imageId = $aLine[0]; | |
my $imageSize = $aLine[1]; | |
if ($hImageSizes{$imageId}) { | |
print "-> imageId $imageId has already been added to the total sum\n"; | |
$prevImageId = $imageId; | |
next; | |
} | |
my $size = sizeExpand($imageSize); | |
# print "$imageId $size\n"; | |
if ($imageId =~ m/missing/) { | |
if ($hImageSizes{$prevImageId}) { | |
# this base image was already added to the sum | |
# print "-> base image imageId $prevImageId has already been added to the total sum \n"; | |
last; | |
} | |
# this is a base image layer, getting the base image total size | |
my $cmd = "docker image ls | grep $prevImageId | head -n 1 | awk '{print \$NF}'"; | |
# print "-> $cmd\n"; | |
my $cmdOut = `$cmd`; | |
# print "-> $cmdOut\n"; | |
$size = sizeExpand($cmdOut); | |
$hImageSizes{$prevImageId} = $size; | |
last; | |
} | |
$hImageSizes{$imageId} = $size; | |
$prevImageId = $imageId; | |
} | |
} | |
for my $imageId (keys(%hImageSizes)) { | |
$totalSize += $hImageSizes{$imageId}; | |
} | |
print "\n\nTOTAL SIZE: ".($totalSize/1000000000)." GB\n"; | |
sub sizeExpand { | |
my ($size) = @_; | |
if ($size =~ m/\dB$/i) { | |
$size =~ s/B$//i; | |
} | |
elsif ($size =~ m/kB$/i) { | |
$size =~ s/kB$//i; | |
$size *= 1000; | |
} | |
elsif ($size =~ m/MB$/i) { | |
$size =~ s/MB$//i; | |
$size *= 1000000; | |
} | |
elsif ($size =~ m/GB$/i) { | |
$size =~ s/GB$//i; | |
$size *= 1000000000; | |
} | |
return $size; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment