DEV Community

Cover image for How I claimed lost disk space due to unused Docker images
Prathamesh Sonpatki
Prathamesh Sonpatki

Posted on

How I claimed lost disk space due to unused Docker images

I started using Docker recently at work. I had to build lot of docker images for my Rails app. After few days, on one fine morning I was building a brand new docker image and my laptop just froze.

Nothing was working

On inspecting the disk size, I noticed that Docker was eating almost 90% of disk space. Fortunately, we can clear the unused space used by Docker very easily.

Find out unused Docker images

Unused Docker images are basically not connected to any of the tagged Docker images. We tag every Docker image to latest tag and push it to Docker Hub. So if we build two images back to back, the latest tag is applied to the most recent one and the older image becomes untagged.
Such unlinked images just consume space and are of no use.

In Docker parlance, such images are called dangling images.

We can find out such images by running this command.

docker images -f "dangling=true" -q
Enter fullscreen mode Exit fullscreen mode

Remove dangling Docker images

Once we find them, just destroy them without a second thought.

Destroy dangling Docker images

docker rmi -f $(docker images -f "dangling=true" -q)
Enter fullscreen mode Exit fullscreen mode

In my experience, just doing this reclaimed most of the wasted disk space. But if you are still facing issues, you can list all the docker images and then selectively delete them.

Sometimes you have to do even more

docker images
docker rmi <id>
Enter fullscreen mode Exit fullscreen mode

Following this trick, I was able to rescue my laptop and use it normally with peace.

Top comments (2)

Collapse
 
oscarteg profile image
Oscar te Giffel

Just use docker system prune or docker system prune -a

Sorry no formatting Iā€™m on mobile

Collapse
 
prathamesh profile image
Prathamesh Sonpatki

Thanks Oscar, it just works!