Docker is a convenient tool for many, but with a few bigger images, the root filesystem becomes sooner or later full. Here is how to quickly and efficiently prune everything regarding Docker on your system to start fresh.
Before I introduce you to the one-liner I use, it’s worth noting that you could run Docker without sudo by adding yourself to the docker group:
$ sudo usermod -aG docker $USER && newgrp docker
We can now check how much space Docker assets take up:
$ docker system df
If that’s not what you hoped for, it’s also worth rechecking that Docker is the biggest offender for running out of disk space. I regularly run the ncdu disk utility (from the ncdu package on Fedora) to quickly assess what takes the most space (sudo ncdu /
).
Once we are confident we need to do something about Docker images and their layers, we can run the following one-liner:
$ docker stop $(docker ps -a -q) && docker system prune -a --volumes -f
Let’s break it down:
docker ps -a -q
list the running container- we need to stop the running containers with
docker stop
so we can truly delete everything - we run
docker system prune -a --volumes -f
to forcefully prune stopped containers, unused networks, dangling images, build cache, and the associated volumes (skip the--volumes
flag if you don’t want that!)
If you only want to remove images rather than networks and volumes, run docker image prune -a -f
instead. If you need more granularity, see the manual.