JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Docker >

Remove old and unused Docker images

Author:JIYIK Last Updated:2025/03/25 Views:

Docker makes it easier for developers to build, test, and deploy applications without worrying about dependencies by packaging them in standardized units called containers. Recently, we have seen widespread adoption of Docker due to its efficiency and other benefits.

Docker images are the basic building blocks of Docker containers and typically consist of code, system tools, libraries, and other dependencies that our application needs to run.

A common way to create images with Docker is to base the image on a base image pulled from a Docker registry.


Remove old and unused Docker images

It’s easy to lose track of the images, volumes, and containers that you’ve created over time. Docker recommends that you eliminate dangling or unused images and containers that may be consuming Docker’s “storage pool” for no reason.

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

There are several methods you can use to remove unused images in Docker. However, before we can remove images, we should be able to list them.

You can use docker imagesthe command to list all the images in the system.

$ 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, we can also use docker imagethe command and ls command to list the images present in the system 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 name and image ID, which you can use to get rid of them.


Removing a single Docker image

Docker only allows you to remove images that are not used by either running or stopped containers. If you try to remove an image used by any container, you will definitely get an error.

docker rmiThe command removes a single Docker image using either the image name 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 delete this image until we get rid of the Docker container. We can docker rm <container_id>delete this container using the command.

Once completed, the image is now unused and can be deleted as shown below.

Code:

$ docker rmi 149077dac3e6

Output:

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

docker rmiWe can also remove multiple unused images by simply listing the names or IDs next to the 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 image. Since these images are unused, they no longer serve any purpose in your system and should be deleted.

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

Code:

$ docker images -f dangling=true

Output:

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

We remove these images using the docker prune command. You will receive a message letting you know that this command will eliminate all dangling images.

If you are sure you want to delete these images, click y for yes.

$ 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 delete all images from the system, including unused images. However, this option should be used with caution and only if you are sure you want to delete all images from the system.

To remove all images, including those that are not used in your system, you first need to list them using docker imagesthe command along with the -q and -a flags. Now, nest this command under docker rmithe 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

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Copy files from host to Docker container

Publish Date:2025/03/25 Views:126 Category:Docker

This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest

Get the IP address of the Docker container

Publish Date:2025/03/25 Views:102 Category:Docker

This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con

Uninstalling Docker on macOS

Publish Date:2025/03/25 Views:95 Category:Docker

Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p

Enter the Docker container's shell

Publish Date:2025/03/25 Views:98 Category:Docker

This article will demonstrate how to enter the Docker container shell using multiple methods. Use docker exec to enter the Docker container's shell We need to have a container up and running to use this command. We can check the status of t

Listing containers in Docker

Publish Date:2025/03/25 Views:122 Category:Docker

This article will discuss various commands for listing containers created in our system. This means we should create some containers beforehand for these commands to return values. List all running containers in Docker We will start by list

Mount the host directory into the Docker container

Publish Date:2025/03/25 Views:189 Category:Docker

Docker provides, among other features, tools to work with the host operating system and the container file system. One of these features is the ability to persist data in containers and share data between containers by mounting directories

Docker build command with multiple parameters

Publish Date:2025/03/25 Views:138 Category:Docker

docker build The command allows us to create a Docker image from a Dockerfile. It also allows us to build an image from a context that references a set of files located in a location specified by a PATH or URL. A Dockerfile, on the other ha

The difference between CMD and ENTRYPOINT in Docker

Publish Date:2025/03/25 Views:141 Category:Docker

Docker containers have become the standard when it comes to managing software and dependencies in different environments. When working with real-world applications, you need to create a docker file before building your application container

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial