Docker.qcow2 stores all the final and intermediary images and containers created by Docker in macOS. It's great, because once an image is stored inside of it, it does not need to be downloaded or built again and data inside containers doesn't get lost either. It's possible to remove it manually from the hard drive if it gets too big or by removing images using docker rmi
. An even easier solution is to use a function, such as the one below, to remove all cached images:
# docker remove all images
function dri(){
docker rmi $(docker images -aq)
}
# docker remove all containers
function drc(){
docker rm $(docker ps -aq)
}
Newer versions of Docker support the following commands with a similar purpose of reclaiming disk space:
# source https://gist.github.com/ngpestelos/4fc2e31e19f86b9cf10b
docker container prune # Remove all stopped containers
docker volume prune # Remove all unused volumes
docker image prune # Remove unused images
docker system prune # All of the above, in this order: containers, volumes, images
These can be added as system functions to be easily available globally in the operating system. But they're rather crude solutions and will delete nearly all images and containers, some of which may still be in use. There are more advanced implementations available on the web, such as this one and this one.
But what if you want to keep Docker data locally but are also limited by disk space? The Docker.qcow2 file can get very large over time, easily larger than 10GB, because Docker doesn't automatically remove any images.
With the newest Docker, you can move Docker.qcow2 to another drive. And it can be an external drive too.
When I tried this option myself, I couldn't change the destination path for Docker.qcow2 from Docker settings. For some unknown reason, Docker would refuse to move Docker.qcow2 from the system drive to the external drive. The new destination was writable and the destination hard drive had enough space but Docker just wouldn't comply with the request. So what I did instead was manually copy the existing Docker.qcow2 file to the new destination, go to Docker's configuration menu and change the configuration to use the copy. This approach fortunately worked. To make sure Docker was actually using the new location to store images, I ran docker build
a few times to check if the file would increase in size after performing these operations, and it did.
I decided to test this solution further. So I moved Docker.qcow2 to a faster external drive while Docker wasn't running and then restarted Docker. It turned out Docker.qcow2 cannot be moved from place to place without using the Docker app. It's possible to copy Docker.qcow2 to another location and reconfigure Docker but if Docker can't find the old file during startup it will show an error message and won't start.
But when I undoed my change and left Docker.qcow2 in both locations I could make the switch in the app and then remove the old file successfully.
It's important to know that the external drive will be blocked from being unmounted while Docker is running and using Docker.qcow2 and Docker won't start if the drive is not available.
(Tested on Docker 17.09.0-ce-mac35)