When working with Docker, it is a good idea to keep on eye on your space management. If you are building and running numerous images, containers and volumes it is quite easy to start using up a lot of space on your local machine.
The following are some Docker commands I use to keep on top of what is happening with Docker:
docker system df
docker ps
docker ps -a
docker rm docker-container-id-goes-here
docker images
docker rmi docker-image-id-goes-here
docker rmi $(docker images -f "dangling=true" -q)
docker volume ls
docker volume prune
docker system prune
It is a good idea to remove unused volumes, images and containers regularly. If you build and re-build image a lot, it is easy to eat up disk space as Docker does not seem to re-use the same volumes. As such, I ended up with 269 unused volumes taking up an incredible amount of space on my machine!
Fortunately, pruning my volumes cleaned up a lot of space that was being taken up unnecessarily.
While we are on the topic of space management, another useful tip for managing space as a developer is:
If you have a lot of projects on your machine that are not actively being worked on, or used regularly, you can delete all of the node_modules
folders in the projects to save a lot of space. Depending on your project, even small projects can take up 300-400MB of space or more via node_modules. This is wasted space if not being used regularly since, replenishing the dependencies is just an npm install
away.
I also found this blog post by Ben McCormick useful in terms of understanding some of what is happening with Docker on Mac. Up until I cleared out my unused images, volumes and networks, the Docker.qcow2 file in the Library/Containers folder on my machine was incredibly big. I did not need to delete that particular file, but once I removed all my unused Docker resource, I did need to restart my machine twice before Docker seems to re-calibrate itself in terms of the amount of space it was using.