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 --volumes -f
Let’s break it down:
docker ps -a -qlist the running container
- we need to stop the running containers with
docker stopso we can truly delete everything
- we run
docker system prune --volumes -fto forcefully prune stopped containers, unused networks, dangling images, build cache, and the associated volumes (skip the
--volumesflag 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.
← BUY THE PRE-RELEASE
I am writing 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. 19 chapters and 2 case studies already available.