Shrink your virtual hard drive file in Docker for MacOS

I really like Docker for MacOS. Not having to explicitly run a virtual machine acting as your Docker host saves a lot of time and headaches. However, Docker for MacOS is still doing just that - running a virtual machine that is your Docker host. And as with all virtual machines, managing disk space is a bit of a one way street. It's easy to grow the file containing your virtual hard drive, but it never shrinks, regardless of how many images you delete.

The file is found here: ~/Library/Containers/com.docker.docker/Data/Docker.qcow2

Luckily, there's an easy fix. Justin Cormack, who is an engineer at Docker has put together a Docker image that shrinks the file for you. To run it, just issue to following command:

~:>docker run --rm --privileged --pid=host justincormack/debian nsenter -t 1 -m -n fstrim /var

Before you do that, you can do a bit of image management by deleting dangling (unused) Docker images:

~:>docker rmi $(docker images --quiet --filter "dangling=true")

EDIT

After getting feedback that this is not working for a lot of people that this isn't working and talking to Justin Cormack on Twitter, he told me that it only works for the latest beta of Docker for Mac (Version 1.13.0-rc3-beta32 (14523)).

He also told me there's a better way of removing dangling images:

~:>docker system prune

comments powered by Disqus
Find me on Mastodon