Complete Guide for Removing Docker Images
A complete article that discusses various scenarios of removing docker images with examples.
If you keep on creating docker images, you’ll soon start to run out of space. Deleting old and unused docker images will free up plenty of disk space for you.
In this article, I’ll discuss various scenarios of deleting docker images from your system.
Ways to remove docker images
First, check the docker images present on your system with this command:
The output will show all the docker images and their image ID. You need this image name (under repository column) or the Image ID to delete a docker image from your system.
abhishek@linuxhandbook:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
debian latest 67e34c1c9477 2 weeks ago 114MB
ubuntu latest 775349758637 6 weeks ago 64.2MB
With the Image ID, you can remove the docker image in the following manner:
docker rmi image_name_or_id
You may also use this command as both are the same:
docker image rm image_id
Here’s what the output may look like:
abhishek@linuxhandbook:~$ docker rmi 67e34c1c9477
If you use the image ID, it will remove all the images associated with that ID.
Remove docker image associated with a container
Life would have been so much simpler if you could just remove docker images like that. But that doesn’t happen often.
If you have containers associated with the docker image, you’ll encounter some errors when you try removing the image.
abhishek@linuxhandbook:~$ docker rmi 775349758637
Error response from daemon: conflict: unable to delete 775349758637 (cannot be forced) - image is being used by running container 13dc0f4226dc
You must stop the container first:
abhishek@linuxhandbook:~$ docker stop 13dc0f4226dc
The problem is that even if you stop the container, it will still complain if you try to remove the image:
abhishek@linuxhandbook:~$ docker rmi 67e34c1c9477
Error response from daemon: conflict: unable to delete 67e34c1c9477 (must be forced) - image is being used by stopped container 5ced86b1fcee
You have two options here:
- Force remove a docker image (associated container will remain in the system)
- Remove the associated container and then remove the docker image
To force remove a docker image, you can use the -f option:
docker rmi -f image_id
To remove a container and then remove the image, you can use something like this:
docker rm container_id
docker rmi image_id
Remove docker image associated with multiple containers
Life would still be a tad bit simpler if a docker image was associated with only one container. But an image can have multiple containers associated with it and removing that kind of docker image becomes a pain.
You’ll see an error like this:
abhishek@itsfoss:~$ docker rmi 775349758637
Error response from daemon: conflict: unable to delete 775349758637 (must be forced) - image is referenced in multiple repositories
First, you need to find all containers associated with an image name (not ID).
docker ps -a -q --filter ancestor=docker_image_name
- -a option displays all running and stopped containers.
- -q option only displays the container ID.
And then you need to stop all of them. You may use the container IDs one by one but that will be too time-consuming. You can use the magic of pipe and xargs to stop all the containers associated with an image:
docker ps -a -q --filter ancestor=ubuntu | xargs docker stop
And then you can either remove the stopped containers or force remove the image (as you saw in the previous section).
If you want to remove all the containers associated with an image, just run this command:
docker ps -a -q --filter ancestor=ubuntu | xargs docker rm
And now you can remove the docker image using the command shown earlier in this tutorial.
Removing multiple docker images at once
You can also remove multiple docker images in one single command. It’s the same as the previous command. You just have to specify the image IDs or the image names.
docker rmi image_id_1 image_id_2 image_id_3
Of course, you’ll have to stop any running containers associated with the images.
Remove all unused and dangling docker images at once
Before you see that, let me explain what is unused and dangling images are:
Any docker image that has any kind of containers associated to it (stopped or running) is a used image. If a docker image has no containers associated, it becomes and unused docker image.
A dangling docker image “means that you’ve created the new build of the image, but it wasn’t given a new name. So the old images you have becomes the dangling image. Those old image are the ones that are untagged and displays <none> on its name when you run ‘docker images’ command.”
If you want to remove the dangling images, you can use the prune option:
docker image prune
If you want to remove both unused and dangling images at once, you can use prune with option -a:
docker image prune -a
You should see the space it frees up at the end of the output:
abhishek@linuxhandbook:~$ docker image prune -a
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 308.3MB
You can be a bit smart with the prune command and remove only old unused and dangling images. So if you want to remove the ones older than 24 hours, use it like this:
docker image prune -a --filter "until=24h"
Remove all docker images from your system
Perhaps you are in a testing environment and you want to start afresh by removing all the docker images.
To remove all docker images, you need to first stop all the running containers.
docker ps -a -q | xargs docker rm
Now you can delete all the images this way:
docker images -a -q | xargs docker rmi -f
That’s it. I think that’s enough reference material for removing docker images and you should have a better understanding of this topic now. You may also check out the tutorial to remove docker containers.
If you have any questions or suggestions, please leave a comment below.