Prune everything with a complete Docker cleanup

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:

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.

← IT'S OUT NOW

I wrote a complete guide on web application deployment. Ruby with Puma, Python with Gunicorn, NGINX, PostgreSQL, Redis, networking, processes, systemd, backups, and all your usual suspects.

More →