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

Docker deploys nginx php application

Publish Date:2025/03/26 Views:131 Category:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

Publish Date:2025/03/26 Views:97 Category:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

Publish Date:2025/03/26 Views:125 Category:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

Publish Date:2025/03/26 Views:202 Category:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Enforce clean build of images in Docker

Publish Date:2025/03/26 Views:88 Category:Docker

This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho

Running a Docker instance from a Dockerfile

Publish Date:2025/03/26 Views:140 Category:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial