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.
Before applying this workaround, you should try
docker system pruneto remove unused data.
As mentioned in this comment, you need at least Docker for Mac version
17.06.0-ce-mac19 and run following commands.
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.
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.
According to this comment, if the above command failed to execute, you can try the following command for newer versions.
docker run --privileged --pid=host justincormack/nsenter1 /sbin/fstrim /var/lib/docker
Hopefully there will be an easier solution for this issue in later Docker for Mac releases.