Docker is a widely used containerization technology. It is simple to design, manage, and deploy lightweight and portable applications with Docker. Docker images are essential to the Docker ecosystem. Basically, they are snapshots of a container that have the code and dependencies that an application needs to run.
The number of Docker images on a system might grow over time, using up important disc space. This can eventually lead to performance issues and a shortage of available disk space. Therefore, it is essential to frequently remove obsolete or unused Docker images. In this article, we will examine how to accomplish this.
Table of Contents
List Docker Images
Before you can delete a Docker image, you need to know its name or ID. To list all the Docker images on your system, use the following command:
This will provide you with a list of all the images on your system, along with their names, tags, and sizes.
Delete a Single Docker Image
To delete a single Docker image, use the
docker rmi command, followed by the name or ID of the image you want to remove. For example, to delete an image with the name
myimageid, you would run:
docker rmi myimageid
If the image has more than one tag, you can specify the tag to remove by appending it to the image name, separated by a colon. For example:
docker rmi myimageid:tag1
This will remove the
myimageid image with the
Delete All Unused Images
Docker also provides a command to remove all images that are not currently in use by a container. This can be useful for cleaning up space on your system. To do this, run the following command:
docker image prune
This will remove all images that are not currently in use by a container. You will be prompted to confirm the deletion before it proceeds.
Delete All Images
If you want to remove all Docker images from your system, you can use the following command:
docker rmi $(docker images -aq)
This command will remove all images on your system. It does this by using the output of
docker images -aq as input to the
docker rmi command. The
q flag tells
docker images to only display the image IDs, which are then passed to
Dealing with any errors
If you get an error when running the rmi command for example something like this:
Error response from daemon: conflict: unable to delete myimageid (must be forced) - image is being used by stopped container mycontainerid
Error response from daemon: conflict: unable to delete myimageid (cannot be forced) - image is being used by running container mycontainerid
This is happening due to the image still being used by an active container (containerid). If you delete the container, then you will be able to remove the image (myimageid).
If you want to force the removal of an image while a container is still using it (not recommended), you must ensure that the Docker container using the image has been stopped.
You will need to find the container name/ID and then stop the container via the terminal. (Out of the scope of this blog).
Once it has been stopped you can use the –force flag in order to remove the image:
docker rmi --force myimageid
Please note this will only remove the image and not the container.
One important part of managing your Docker environment is getting rid of old or unused Docker images. It helps to keep your system running smoothly and ensures that you have enough space for new images. By using the
docker rmi and
docker image prune commands, you can easily remove images that you no longer need.