删除旧的和未使用的 Docker 镜像
Docker 通过将应用程序包装在称为容器的标准化单元中,使开发人员可以更轻松地构建、测试和部署应用程序,而无需担心依赖性。 最近,由于 Docker 的效率和其他优势,我们看到了 Docker 的广泛采用。
Docker 镜像是 Docker 容器的基本构建块,通常由代码、系统工具、库和我们的应用程序运行所需的其他依赖项组成。
使用 Docker 创建镜像的一种常见方法是将图像基于从 Docker 注册表中提取的基本镜像。
删除旧的和未使用的 Docker 镜像
很容易忘记随着时间的推移创建的镜像、卷和容器。 Docker 建议我们消除可能无缘无故消耗 Docker 的“存储池”的悬空或未使用的镜像和容器。
悬挂的 Docker 镜像主要由系统中您完全忘记的旧的未命名镜像组成。 另一方面,未使用的镜像是没有被任何 Docker 容器使用的镜像。
您可以使用多种方法来删除 Docker 中未使用的镜像。 但是,在我们可以删除镜像之前,我们应该能够列出它们。
您可以使用 docker images
命令列出系统中的所有镜像。
$ 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
或者,我们也可以使用 docker image
命令和 ls 命令列出系统中的图像,如下所示。
$ 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
列出 Docker 映像可以让您访问 Docker 名称和映像 ID,您可以使用它们来摆脱它们。
移除单个 Docker 镜像
Docker 只允许您删除既未被正在运行的容器使用也未被停止的容器使用的镜像。 如果你试图删除任何容器使用的镜像,你肯定会得到一个错误。
docker rmi
命令使用图像名称或图像 ID 删除单个 Docker 镜像。
$ docker rmi 149077dac3e6
Error response from daemon: conflict: unable to delete 149077dac3e6 (must be forced) - image is being used by stopped container 841d1e8d8c25
这意味着在我们摆脱 Docker 容器之前,我们不能删除这个图像。 我们可以使用 docker rm <container_id>
命令删除这个容器。
完成后,该图像现在未使用,可以删除,如下所示。
代码:
$ docker rmi 149077dac3e6
输出:
Untagged: new-image:latest
Deleted: sha256:149077dac3e6f61c31ca98da741afd5d36147b69cacd945e3d53bd763ec7b420
我们还可以通过简单地在 docker rmi
命令旁边列出名称或 ID 来删除多个未使用的图像。
$ docker rmi ubuntu rabbitmq
输出:
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
移除悬挂的 Docker 镜像
悬挂图像是与任何标记图像都没有关系的图像。 由于这些图像未使用,因此它们在您的系统中不再有任何用途,应将其删除。
我们可以使用 -f 标志列出悬挂图像,并在下面的 docker images
命令旁边设置 dangling=true 。
代码:
$ docker images -f dangling=true
输出结果:
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 22fa358b711d 2 weeks ago 929MB
我们使用 docker prune 命令删除这些镜像。 您将收到一条消息,提示您此命令将消除所有悬挂图像。
如果您确定要删除这些图像,请单击 y 表示是。
$ docker image prune
输出:
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
最后,您还可以删除系统中的所有图像,包括未使用的图像。 但是,应谨慎使用此选项,并且仅当您确定要删除系统中的所有图像时才使用。
要删除所有图像,包括系统中未使用的图像,您首先需要使用 docker images
命令以及 -q 和 -a 标签列出它们。 现在,将此命令嵌套在下面的 docker rmi
命令下。
代码:
$ docker rmi $(docker images -q -a)
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:bea6d19168bbfd6af8d77c2cc3c572114eb5d113e6f422573c93cb605a0e2ffb
Deleted: sha256:ff0fea8310f3957d9b1e6ba494f3e4b63cb348c76160c6c15578e65995ffaa87
Deleted: sha256:867d0767a47c392f80acb51572851923d6d3e55289828b0cd84a96ba342660c7
Deleted: sha256:22fa358b711d2ea3a1d72e59f062f6c7c38b414bdb253fb8d0def20222cadd93
相关文章
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
发布时间:2025/03/26 浏览次数:165 分类:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
发布时间:2025/03/26 浏览次数:131 分类: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
发布时间:2025/03/26 浏览次数:107 分类: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
发布时间:2025/03/26 浏览次数:97 分类: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
发布时间:2025/03/26 浏览次数:124 分类: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
发布时间:2025/03/26 浏览次数:202 分类: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
发布时间:2025/03/26 浏览次数:87 分类: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
发布时间:2025/03/26 浏览次数:140 分类: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