maciej łebkowski Maciej Łebkowski

Cleaning up docker to reclaim disk space

in Professional

I’m a big fan of docker. For more than two years now I believe it will change how we deploy applications. Not only web apps, but CLI tools as well. Maybe even GUI apps. That point of view doesn’t change the fact that I see a lot of drawbacks.

One of the main things that bother me when using docker is it hogging up disk space. Recently I constantly ran into issues with my setup because the disk space was „leaking” somewhere and I couldn’t tell where it went. Here are some tips and solutions how to avoid keeping unused volumes behind or prevent them being left in the first place.

Good practices regarding keeping disk usage to a minimum

First of all, docker by default doesn’t care about using the disk space. Most of the commands leave a trace behind, make a copy of something or replace an item without removing the previous version. Let’s take a look at the most common ones:

Docker filesystem storage and volumes

There are three main ways docker stores files:

Now, since there is no tool to list volumes and their state, it’s easy to leave them on disk even after all processes exited and all containers are removed. The following command inspects all containers (running or not) and compares them to created volumes, printing only the paths that are not referenced by any container:

#!/usr/bin/env bash

find '/var/lib/docker/volumes/' -mindepth 1 -maxdepth 1 -type d | grep -vFf <(
  docker ps -aq | xargs docker inspect | jq -r '.[]|.Mounts|.[]|.Name|select(.)'

What it does, step by step:

You need to run this as root and have jq utility present.

The command doesn’t remove anything, but simply passing the results to xargs -r rm -fr does so.

Hint: docker 1.9 has new volume management system, so it’s way easier with this version:

docker volume ls -qf dangling=true | xargs -r docker volume rm


Save the following script to clean up everything at once:


# remove exited containers:
docker ps --filter status=dead --filter status=exited -aq | xargs -r docker rm -v

# remove unused images:
docker images --no-trunc | grep '<none>' | awk '{ print $3 }' | xargs -r docker rmi

# remove unused volumes (needs to be ran as root):
find '/var/lib/docker/volumes/' -mindepth 1 -maxdepth 1 -type d | grep -vFf <(
  docker ps -aq | xargs docker inspect | jq -r '.[]|.Mounts|.[]|.Name|select(.)'
) | xargs -r rm -fr

This maintenance should trim all the fat and reduce your docker environment only to the used containers, images, and volumes.

Creative Commons License

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License . This means that you may use it for commercial purposes, adapt upon it, but you need to release it under the same license. In any case you must give credit to the original author of the work (Maciej Łebkowski), including a URI to the work.