Clean up disk space of Docker for Mac

After running Docker for Mac for a long time, Docker keeps eating up disk space. Even though the containers and images are removed, the disk space is not reclaimed. The problem is the file /Users/<username>/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2 is too big. For my Mac, this file is about 46G. This is actually a known issue tracked in this GitHub issue. So far, we only have a workaround to fix this issue.

As mentioned in this comment, you need at least Docker for Mac version 17.06.0-ce-mac19 and run following commands.

1
2
3
4
5
6
7
8
9
10
11
12
cd ~/Library/Containers/com.docker.docker/Data/database/
git checkout master
git reset --hard
mkdir -p com.docker.driver.amd64-linux/disk
echo 262144 > com.docker.driver.amd64-linux/disk/compact-after
echo 262144 > com.docker.driver.amd64-linux/disk/keep-erased
echo -n true > com.docker.driver.amd64-linux/disk/trim
git add com.docker.driver.amd64-linux/disk/compact-after
git add com.docker.driver.amd64-linux/disk/keep-erased
git add com.docker.driver.amd64-linux/disk/trim
git commit -s -m 'Enable on-line compaction'

Then force the trim operation to run.

1
docker run --rm --net=host --pid=host --privileged -it justincormack/nsenter1 /sbin/fstrim /var

Now we can wait and see the size of file Docker.qcow2 shrinks. For my Mac, the size of this file shrinks to about 12G. The script actually creates the cron job to run the trim operation, so you need to do run this manually.

Hopefully there will be an easier solution for this issue in later Docker for Mac releases.

Comments