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.
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.
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:
docker rmi Image_id removes the specific docker image from your host. For this demonstration we removed the Node image with the hash/id:
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.
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.
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”.
Click on the local setup!
Now, click on Images and then you would be in the dashboard!
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.
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: