Docker

Complete Guide for Removing Docker Images

A complete article that discusses various scenarios of removing docker images with examples.

Abhishek Prakash
Abhishek Prakash

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:

docker images

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.

[email protected]:~$ 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:

[email protected]:~$ docker rmi 67e34c1c9477
Untagged: debian:latest
Untagged: [email protected]:79f0b1682af1a6a29ff63182c8103027f4de98b22d8fb50040e9c4bb13e3de78
Deleted: sha256:67e34c1c9477023c0ce84c20ae5af961a6509f9952c2ebbf834c5ea0a286f2b8
Deleted: sha256:f2b4f0674ba3e6119088fe8a98c7921ed850c48d4d76e8caecd7f3d57721b4cb

If you use the image ID, it will remove all the images associated with that ID.

Remove Docker Images

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.

[email protected]:~$ 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:

[email protected]:~$ docker stop 13dc0f4226dc
13dc0f4226dc

The problem is that even if you stop the container, it will still complain if you try to remove the image:

[email protected]:~$ 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:

[email protected]:~$ 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:

[email protected]:~$ 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
Deleted Images:
untagged: ubuntu:latest
untagged: [email protected]:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d
untagged: debian:latest
untagged: [email protected]:79f0b1682af1a6a29ff63182c8103027f4de98b22d8fb50040e9c4bb13e3de78
deleted: sha256:67e34c1c9477023c0ce84c20ae5af961a6509f9952c2ebbf834c5ea0a286f2b8
deleted: sha256:f2b4f0674ba3e6119088fe8a98c7921ed850c48d4d76e8caecd7f3d57721b4cb
untagged: fedora:latest
untagged: [email protected]:d4f7df6b691d61af6cee7328f82f1d8afdef63bc38f58516858ae3045083924a
deleted: sha256:f0858ad3febdf45bb2e5501cb459affffacef081f79eaa436085c3b6d9bd46ca
deleted: sha256:2ae3cee18c8ef9e0d448649747dab81c4f1ca2714a8c4550eff49574cab262c9

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.



Join the conversation.