Docker images can take up considerable disk space on your host system or in your container registry. By deleting an unused or obsolete Docker image, you free up space for new images and data. This is particularly useful in environments where available space is limited.
When should you delete a Docker image?
Docker images are created from what are called Dockerfiles, which define the steps for configuring the container infrastructure. These images can then be used to launch Docker containers that run the application or service in an isolated, consistent environment.
However, a high number of images can hurt the performance of your Docker infrastructure and take up excessive storage space. When Docker searches for an image, it must go through all available images to find the correct one. By removing an unnecessary Docker image, you therefore increase efficiency and reduce search time. This also protects your system from security risks related to old images.
Finally, when the development and testing cycles are completed and you have used images for temporary purposes, it is recommended to delete them to free up space.
IONOS Cloud Compute Engine
Medium and large companies choose Cloud Made in Germany. IaaS and PaaS from hidden champion to hidden champions.
If you want to delete one or more Docker images at once, you can pass a list of image IDs or names to the command docker rmi
.
Step 1: List image IDs and tags
The following command displays a list of all Docker images on your system:
The output contains information such as image name, image ID, creation date, size, and image creator.
Step 2: Delete Images
If you want to delete multiple images at the same time, you can write the image IDs or image names separated by spaces one behind the other.
docker rmi Image Image
bash
Please note: you can only delete a Docker image if no active container depends on it. So you must first stop and delete the containers before deleting the images associated with them.
Deleting unreferenced images refers to deleting Docker images that are no longer used by active containers or other images. These unreferenced or unused images can accumulate over time and take up space on your Docker host. A good practice is to delete these images at regular intervals to improve resource management of your Docker environment
Step 1: List unreferenced images
You can start by viewing a list of unreferenced images to check which images should be removed.
docker images -f dangling=true
bash
Step 2: Remove Unreferenced Images
The following command deletes all unreferenced images and frees the associated disk space.
##How to delete Docker images according to a template?
Deleting Docker images according to a specific model or prefix allows you to delete a group of images at once if they match a common naming pattern. This is especially useful if you have a large number of images and only want to delete a certain category of them.
Step 1: List Docker images with grep
You can combine order docker images
with grep
to display only images that match your template.
docker images -a | grep "pattern"
bash
Step 2: Delete Docker Images
Once you have chosen a template, you can filter all the images that match it with awk
to extract the IDs from the images, and finally delete them with xargs docker rmi
.
docker images -a | grep "pattern" | awk '{print $3}'. | xargs docker rmi
bash
Deleting all Docker images from your system should be done with extreme caution, as it is a irreversible stage which may result in not being able to start all of your containers.
Step 1: List All Images
The first thing to do is to have an overview of all the active and inactive images of your system using the following command:
Step 2: Delete All Docker Images
You can then pass the output from the previous step to the command docker rmi
. This deletes all images.
docker rmi $(Images Docker -a -q)
bash
You can find more tips on deleting a Docker volume or deleting a Docker container in our guide. In the IONOS Digital Guide we also explain what Docker is and how Docker containers and Docker container volumes are different.