Shrink docker’s VirtualBox machine for mac

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.

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 .vdi image.

This guide assumes your docker virtual machine is called “default”.

  1. Log into the the docker machine and execute a command to fill all the free space on the disk with 0’s.


    This creates a file zero.fill in /mnt/sd1/tmp and 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.

  2. Back on the host system, stop the running boot2docker virtual machine so the image can be compacted.

  3. Navigate to the directory where your docker machine is kept.

  4. Convert the .vmdk image to .vdi using the clone command


    – If you do not have enough disk space on your machine, consider using external storage and change any reference of disk.vdi to /Volumes/<external-drive-name/disk.vdi.
    – This will take several minutes. Time to make a cup of tea.

  5. Compact the .vdi image

  6. Get the UUID for the original disk.vmdk disk image, and write it down.


    ! Make a note of the UUID
    This will be needed later when converting the .vdi image back into a .vmdk image.

  7. Remove to original disk.vmdk

  8. Convert the .vdi image back into a .vmdk image

  9. Reset the UUID of the new disk.vmdk image to be the same as the original disk.vmdk UUID

  10. Restart the docker virtual machine

  11. Lastly, remove the disk.vdi file as it is no longer needed.

    All done.

    Checking the size of the virtual disk, it’s now only 3.4GB. That’s almost 85% smaller! Over time it will increase again and you’ll need to go through this same routine.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.