Over time, the boot2docker virtual machine image
disk.vmdk will consume more and more disk space as each disk sector is written to for the first time (up until its maximum virtual disk size or the host system runs out of resources).
There are a couple of approaches to reduce the size of the virtual box image. First approach is to delete the virtual machine and then reinstall it. Alternatively you can shrink the virtual machine disk.
This guide demonstrates how to shrink the boot2docker virtual machine image. It is intended for reference only and you should always make sure you have appropriate backups first before attempting to do this.
Before you begin
Docker can leave being dangling images when using the
docker commands. These images build up taking consume additional space, so before we begin, lets remove all the containers that are in an
exited state as well as any dangling images.
docker rm $(docker ps -a -q --filter status=exited); docker images --quiet --filter=dangling=true | xargs docker rmi -f;
Shrinking the VirtualBox image
This approach will take the
.vmdk VirtualBox image, convert it into a
.vdi image, compact it and then convert it back to
.vmdk. Sadly there are no tools included within VirtualBox to compact a
.vmdk image directly
The drawback with the approach is that you need to have enough free disk space on the host machine to clone the image. If you do not have enough free disk space then you will need to use external storage for storing the temporary
This guide assumes your docker virtual machine is called “default”.
- Log into the the docker machine and execute a command to fill all the free space on the disk with 0’s.
1docker-machine ssh default
12cat /dev/zero > /mnt/sda1/tmp/zero.fill; sync; sleep 1; sync; rm -f /mnt/sda1/tmp/zero.fill;cat /dev/zero > zero.fill; sync; sleep 1; sync; rm -f zero.fill;
This creates a file
/mnt/sd1/tmpand the current working directory. The purpose of this file is to force all the data bits in available free space to be reset to
0. By doing this, VirtualBox will be able to compact that space thus reducing the boot2docker virtual machine size.
Note: You will receive the error message cat: write error: No space left on device that the device has run out of space, this is expected.
Exit the docker machine host.1exit
- Back on the host system, stop the running boot2docker virtual machine so the image can be compacted.
1VBoxManage controlvm default poweroff soft
- Navigate to the directory where your docker machine is kept.
- Convert the
.vdiusing the clone command1VBoxManage clonehd disk.vmdk disk.vdi --format vdi --variant Standard
– If you do not have enough disk space on your machine, consider using external storage and change any reference of
– This will take several minutes. Time to make a cup of tea.
- Compact the
.vdiimage1VBoxManage modifyhd disk.vdi --compact
- Get the UUID for the original
disk.vmdkdisk image, and write it down.1VBoxManage showhdinfo disk.vmdk
! Make a note of the UUID
This will be needed later when converting the
.vdiimage back into a
- Remove to original
- Convert the
.vdiimage back into a
.vmdkimage1VBoxManage clonehd disk.vdi disk.vmdk --format vmdk --variant Standard
- Reset the UUID of the new
disk.vmdkimage to be the same as the original
disk.vmdkUUID1VBoxManage internalcommands sethduuid disk.vmdk <original-uuid-here>
- Restart the docker virtual machine
1VBoxManage startvm default --type headless
- Lastly, remove the disk.vdi file as it is no longer needed.