docker images clean

How to clean up disk space occupied by Docker images?

by | 18.10.2021 | Engineering

Docker has revolutionised containers even if they weren’t the first to walk the path of containerisation. The ease and agility docker provide makes it the preferred engine to explore for any beginner or enterprise looking towards containers.

The one problem most of the people I talk to is keeping track of storage (images, volumes) in the isolated environment and making sure your storage space is not over utilised. If you complain about the same storage issues, this blog post will free you from the “not enough storage” dilemma.

Let’s start!

Storage Stats

Running docker system df would list all the storage used by Docker in its isolated environment. The details include images, containers changing the file system, build cache, and Volumes which you can walk through in the bottom Image. However, we don’t include the storage used by bind volumes on the host OS.

Figure: Storage occupied by docker environment

Deleting Images

Running docker image ls would list all of the images available on your host OS. This command is helpful in the context of deleting images when you’re targeting some specific image. Having the ID from the Image ID field helps you to delete a specific Image.

Delete Specific Image with ID:

Running docker rmi Image_id removes the specific docker image from your host. For this demonstration we removed the Node image with the hash/id: c4fbd63932640bbb14c76f80c5b53daf8f01cc1f011d260d07f35bb6874c0c30.

You don’t need to plug all characters of the SHA256 (cryptographic hash function) of any Image. To delete, just put the first few initial unique characters of the Image ID. For deleting Node Image, we used the first three characters, and deletion was initiated.

The image id is equal to the sha256 hash function of the latest layer of the Image. Each version of an image is stored separately in Docker, but it’s organised in layers so that many new versions don’t take up twice as much space.

Docker Subcommands

The prune subcommand used as docker image prune offers more functionalities than the traditional one tag one image deletion. Using docker image prune is powerful and removes all images that have no relationship to any tagged images or aka dangling Images with <None> tags.

But what if you want to delete all images alongside all these dangling Images? Using docker image prune with the -a flag removes (docker image prune -a) all images not associated with at least one container alongside everything the traditional prune command does.

Limitations

Docker provides commands that are too powerful (prune) or commands too focused, like rmi, which deletes one image at a time after you have the IDs. There’s no middle ground, and my use case requires me to be in the middle ground.

I have explored few tools that focus on cleaning your storage spaces, and sometimes they come with extra functionalities or one. Portainer stands out to be a great opensource tool with a small memory footprint and extra functionalities that help you carry your journey with Docker, Kubernetes, ACI and other members of the DevOps family with functionalities that simplify your journey. We would go forward with the image clean up using Portainer.

Portainer: Simplifying Cleaning

Portainer comes in the form of a docker container, and installing it is as straightforward as pulling the image from the docker hub and then running the container with mounting volumes and creating a user. You can read this tutorial on Installing Portainer on Ubuntu or explore the official documentation to run Portainer on Mac, Windows Container Service, Linux or Windows and follow the tutorial.

Navigating to Docker Installation

Once you are done with the initial setup for the Portainer server, you can click on Docker and connect to the endpoint for Docker by selecting Docker and clicking on “⚡Connect”.

Docker Endpoint Portainer
Docker Endpoint Portainer

Click on the local setup!

Portainer Environments and only GUI with Dark Mode 😉

Now, click on Images and then you would be in the dashboard!

Navigate to Images

Cleaning Up

Now, you are presented with an Intuitive experience of deleting all the images you want. Check the boxes you want to delete or select the master option (1) and check all the boxes and uncheck which you don’t want to delete. You must ensure which images are currently not being used while deleting the Images, and you can easily do that by looking at the tags beside ID (2) on Portainer Dashboard.

Delete using a click on Remove. (3)

Now, the best part of portainer is that if you explore the options on the left panel (Eg: Volumes) and follow the same steps it won’t take anything extra to delete them. No extra commands just productive work.

Final Thoughts

By the end of this post, I hope you will be able to delete images very quickly and intuitively like a pro user. Occasional cleaning would save you a couple of gigabytes of storage. There’s a force remove option with escalated privileges, and you can stop containers navigate and create more containers using Portainer. It also supports GitOps and managing Kubernetes.

If you feel exploratory feel free to read more posts from us here:

Happy Cleaning!

CommunityNew

The DevOps Awareness Program

Subscribe to the newsletter

Join 100+ cloud native ethusiasts

#wearep3r

Join the community Slack

Discuss all things Kubernetes, DevOps and Cloud Native

Related articles6

Introduction to GitOps

Introduction to GitOps

GitOps serves to make the process of development and operations more developer-centric. It applies DevOps practices with Git as a single source of truth for infrastructure automation and deployment, hence the name “Git Ops.” But before getting deeper into what is...

Kaniko: How Users Can Make The Best Use of Docker

Kaniko: How Users Can Make The Best Use of Docker

Whether you love or hate containers, there are only a handful of ways to work with them properly that ensures proper application use with Docker. While there do exist a handful of solutions on the web and on the cloud to deal with all the needs that come with running...

Cilium: A Beginner’s Guide To Improve Security

Cilium: A Beginner’s Guide To Improve Security

A continuation from the previous series on eBPF and security concerns; it cannot be reiterated enough number of times how important it is for developers to ensure the safety and security of their applications. With the ever expanding reach of cloud and software...

Parsing Packages with Porter

Parsing Packages with Porter

Porter works as a containerized tool that helps users to package the elements of any existing application or codebase along with client tools, configuration resources and deployment logic in a single bundle. This bundle can be further moved, exported, shared and distributed with just simple commands.

eBPF – The Next Frontier In Linux (Introduction)

eBPF – The Next Frontier In Linux (Introduction)

The three great giants of the operating system even today are well regarded as Linux, Windows and Mac OS. But when it comes to creating all purpose and open source applications, Linux still takes the reign as a crucial piece of a developer’s toolkit. However, you...

Falco: A Beginner’s Guide

Falco: A Beginner’s Guide

Falco shines through in resolving these issues by detecting and alerting any behaviour that makes Linux system calls. This system of alerting rules is made possible with the use of Sysdig’s filtering expressions to detect potentially suspicious activity. Users can also specify alerts for specific calls, arguments related to the calls and through the properties of the calling process.