I am using docker for container image build and deployment for almost 2–3 years and sometimes I struggled to get my storage back. By default, docker does not clear unused objects such as images, containers, networks, and volumes. This causes a high amount of disk usage by Docker.
In this post, I will try to cover quick steps to analyze and clean up this data.
You can list the statistics of docker file system usage by inbuilt command -
arun@controller:~$ sudo docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 15 0 2.674GB 2.674GB (100%)
Containers 0 0 0B 0B
Local Volumes 0 0 0B 0B
Build Cache 0 0 0B 0B# use -v for more details
It will list how docker is using your storage and if you can reclaim something back or not!
Now we know where is our storage! Basically, there are 4 main categories where you can look for a cleanup option.
Big bang approach
docker system prune is the best way to clean up multiple types of objects at once. This approach does not bother about anything and straightway starts the cleanup.
$ docker system prune# include -a to remove all unused images not just dangling ones.# You have an option of putting filter as well.$ docker image prune -a --filter "until=24h"
The unused word here refers to the object, that is not referenced by any container
Maybe you don't believe in the big bang, even I don't. I always love to perform things in a controlled fashion.
Prune is again there to help.
$ docker image prune -a
$ docker container prune -a
$ docker network prune -a
$ docker volume prune -a
I - Command line!
When I want to stop all of the running containers and remove them.
$ docker stop $(sudo docker ps -aq) && sudo docker rm $(sudo docker ps -aq)
When I want to remove all of the Images
$ docker rmi $(sudo docker images -q)# dangling
$ docker rmi $(docker images -f "dangling=true" -q)
$ docker volume rm $(docker volume ls -q -f dangling=true)
$ docker rm $(docker ps -qa --no-trunc --filter "status=exited")
In one situation, I was not able to retrieve all my disk space after running prune and the other commands. Then, I checked the default location of the docker and verified the size of the folder. It had a lot of data (probably in overlay)
$ cd /var/lib/docker
$ du -sh .
I struggled to clean that up. I uninstalled and installed docker again. 😎
Keep Learning and Stay Safe 🌎