Tri Nguyen
Tri Nguyen

Reputation: 11098

how to reduce the size of Vagrant VM image

I started a VM from this image https://atlas.hashicorp.com/bento/boxes/centos-6.7, which is only about 2-300MB in size. I then installed a few packages (java, node, docker etc.) in order to create a faster starting image for my app.

I then run yum clean all and vagrant package --output newimage.box, but the newimage.box is ~3GB in size.

I've been looking around for a way to compact/ reduce the size of this image, but didn't find a lot. I found some mention of zerofree, but have been unable to install it on the CentOS box.

Any suggestions/ advice here would be really appreciated.

Upvotes: 1

Views: 4858

Answers (2)

kotelcat
kotelcat

Reputation: 11

  1. Zerofree should be very easy to compile, it's a single c file with a dependency on ext2. (official site https://frippery.org/uml/)

  2. In my experience working with vdi files, the size wouldn't change much before running VBoxManage's compact command. It would appear vagrant uses VDMK disks. VirtualBox afaik does not support compacting VDMKs but this workaround might work https://stackoverflow.com/a/35620550/7217931 . VMware workstation can supposedly do it out of the box.

I would have rather posted it as a comment but apparently, you need more reputation for that.

Upvotes: 1

Frederic Henri
Frederic Henri

Reputation: 53733

you can look at a cleanup script example used from packer to shrink image size.

The important points are

echo "==> Clean up yum cache of metadata and packages to save space"
yum -y --enablerepo='*' clean all

echo "==> Clear core files"
rm -f /core*

echo "==> Removing temporary files used to build box"
rm -rf /tmp/*

echo '==> Zeroing out empty area to save space in the final image'
dd if=/dev/zero of=/EMPTY bs=1M
rm -f /EMPTY

Upvotes: 6

Related Questions