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