1

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.

Tri Nguyen
  • 9,950
  • 8
  • 40
  • 72

2 Answers2

6

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
Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
  • Thanks! That helped reducing its size to 1.85GB. Do you think that is the smallest it would get, or is there a way to compact it further? – Tri Nguyen Mar 01 '16 at 21:20
  • you can check with `df -h` for the disk usage, depending all what you installed it might be the smallest – Frederic Henri Mar 01 '16 at 21:23
  • `df -h` reports as `/dev/sda1 477M 56M 396M 13% /boot`. It just seems weird to me that installing those packages would've increased the image size like 8-9 folds. – Tri Nguyen Mar 01 '16 at 21:46
1
  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.

kotelcat
  • 11
  • 1
  • 2