How to Remove Old and Unused Docker Images

Isaac Tony Feb 02, 2024
  1. Remove Old and Unused Docker Images
  2. Remove a Single Docker Image
  3. Remove Dangling Docker Images
How to Remove Old and Unused Docker Images

Docker makes it easier for developers to build, test and deploy applications without worrying about dependencies by wrapping them in standardized units called containers. Over the recent past, we have seen extensive Docker adoption due to its efficiency, among other benefits.

Docker images are fundamental building blocks of Docker containers and usually consist of code, system tools, libraries, and other dependencies that our application would require to run.

A common way of creating images using Docker is basing images on base images drawn from the Docker registry.

Remove Old and Unused Docker Images

It is easy to lose track of the images, volumes, and containers created over time. Docker recommends that we do away with dangling or unused images and containers that may consume Docker’s “storage pool” for no good reason.

Dangling Docker images mostly consist of old unnamed images in your system that you completely forgot about. On the other hand, unused images are images that are not being used by any Docker container.

You can use several methods to get rid of unused images in Docker. However, before we can remove images, we should be able to list them.

You can list all the images in your system using the docker images command.

Code:

$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
new-image    latest    149077dac3e6   2 hours ago   932MB
<none>       <none>    22fa358b711d   2 weeks ago   929MB
myapp        latest    ee771b73a9ec   4 weeks ago   929MB
rabbitmq     latest    d4455d35bc06   8 weeks ago   221MB

Alternatively, you can also list images in your system using the docker image command alongside the ls command, as shown below.

$ docker image ls
new-image    latest    149077dac3e6   2 hours ago   932MB
<none>       <none>    22fa358b711d   2 weeks ago   929MB
myapp        latest    ee771b73a9ec   4 weeks ago   929MB
rabbitmq     latest    d4455d35bc06   8 weeks ago   221MB

Listing Docker images gives you access to the Docker names and image IDs that you can use to get rid of them.

Remove a Single Docker Image

Docker only allows you to remove images that are neither used by a running nor a stopped container. If you attempt to remove an image used by any container, you will certainly get an error.

The docker rmi command removes a single Docker image using the name of the image or the image id.

$docker rmi 149077dac3e6
Error response from daemon: conflict: unable to delete 149077dac3e6 (must be forced) - image is being used by stopped container 841d1e8d8c25

This means that we cannot remove this image until we have gotten rid of the Docker container. We can remove this container using the docker rm <container_id> command.

Once that is done, the image is now unused and can be removed, as shown below.

Code:

$ docker rmi 149077dac3e6

Output:

Untagged: new-image:latest
Deleted: sha256:149077dac3e6f61c31ca98da741afd5d36147b69cacd945e3d53bd763ec7b420

We can also remove multiple unused images by simply listing the names or IDs alongside the docker rmi command.

$ docker rmi ubuntu rabbitmq

Output:

Untagged: ubuntu:latest
Untagged: ubuntu@sha256:bea6d19168bbfd6af8d77c2cc3c572114eb5d113e6f422573c93cb605a0e2ffb
Deleted: sha256:ff0fea8310f3957d9b1e6ba494f3e4b63cb348c76160c6c15578e65995ffaa87
Deleted: sha256:867d0767a47c392f80acb51572851923d6d3e55289828b0cd84a96ba342660c7
Untagged: rabbitmq:latest
Untagged: rabbitmq@sha256:3d4c70ec5fc84c27efaeb56c50aafcac4fd8583b61398cc028e4876f84ae73d8
Deleted: sha256:d4455d35bc062a1c1847c2e83b8fae2f40a83075aad536f8bf82166c71431ad2
Deleted: sha256:84693641bf34ab0dee198b5b04c94c4c295626a4d29aacdeb8d17eaf200502ac
Deleted: sha256:8f76417ffbedd6e87b802960c31571aa49d14b058505475712e6ce8ee676718c
Deleted: sha256:a2fd31c374592ebd2e920f312aab1b27e592989a8af371c430fb8f915365bfb0
Deleted: sha256:b1d41dbdcd3cfe9eff61d43ecba57adf40bd26853fe2c7ab203f6f3bfbbe2761
Deleted: sha256:3560f714926f60121a89674e17510e4044f70be3229953fbbd82cb4eea6b1153
Deleted: sha256:4cd230fe05650d13ec67f7edde60fad03ab6cea3db52df798680294632ff62d3
Deleted: sha256:e535128bae717fe882e77c5283b08840efbf73e07eb65b1ef11df14ed4ea911f
Deleted: sha256:15d050b0b911cf148f48c6fb9ca3d654af4855504aee7791c7f7fce1c9fe1b21
Deleted: sha256:36ffdceb4c77bf34325fb695e64ea447f688797f2f1e3af224c29593310578d2

Remove Dangling Docker Images

Dangling images are images that have no relationship to any tagged images. Since these images are unused, they no longer serve any purpose in your system and should be removed.

We can list dangling images using the -f flag and set dangling=true alongside the docker images command below.

Code:

$ docker images -f dangling=true

Output:

REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
<none>       <none>    22fa358b711d   2 weeks ago   929MB

We use the docker prune command to remove these images. You will get a message prompting you that this command will eliminate all the dangling images.

Click y for yes if you’re certain you want to get rid of these images.

$ docker image prune

Output:

WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

Finally, you can also eliminate all images in your system, including the unused images. However, this option should be used with caution, and only when certain you surely want to eliminate all images in your system.

To remove all images, including the unused images in your system, you’ll first need to list them using the docker images command and the -q and -a tags. Now, nest this command under the docker rmi command below.

Code:

$ docker rmi $(docker images -q -a)
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:bea6d19168bbfd6af8d77c2cc3c572114eb5d113e6f422573c93cb605a0e2ffb
Deleted: sha256:ff0fea8310f3957d9b1e6ba494f3e4b63cb348c76160c6c15578e65995ffaa87
Deleted: sha256:867d0767a47c392f80acb51572851923d6d3e55289828b0cd84a96ba342660c7
Deleted: sha256:22fa358b711d2ea3a1d72e59f062f6c7c38b414bdb253fb8d0def20222cadd93
Author: Isaac Tony
Isaac Tony avatar Isaac Tony avatar

Isaac Tony is a professional software developer and technical writer fascinated by Tech and productivity. He helps large technical organizations communicate their message clearly through writing.

LinkedIn

Related Article - Docker Image