迹忆客 EN >

当前位置:主页 > 学无止境 > 操作系统 > Docker >

删除 Docker 中的本地镜像

作者:迹忆客 最近更新:2023/03/22 浏览次数:

本文将介绍在 Docker 中删除本地镜像的方法。

删除 Docker 中未使用和悬空的本地映像

假设除了有未标记的 docker 镜像之外,你还有未使用的镜像和一些你还想删除的容器。在这种情况下,请选择下面显示的命令。

$ docker system prune -a

此命令将提示警告,让你知道如果你选择是,此命令将:

  • 删除所有停止的容器;
  • 删除至少一个容器未使用的所有网络;
  • 删除所有没有至少一个与之关联的容器的镜像;
  • 删除所有缓存。

但是,如果此命令不符合你需要完成的内容,并且你只想删除:

  • 未标记或悬空的镜像;
  • 停止的容器;
  • 悬空缓存;
  • 网络未被至少一个容器使用。

在这种情况下,使用 prune 命令而不包括 -a 标签,如下所示。

$ docker system prune

删除 Docker 中的特定本地映像

我们可能还想从本地系统中删除特定镜像,并保持所有其他文件不变。

在这种情况下,我们需要一个我们想要删除的特定镜像的镜像 ID。我们可以通过 Docker 桌面应用程序中的镜像页面访问它。

使用 Docker Desktop 访问镜像 ID

我们还可以通过运行以下命令,使用我们的终端或 Docker CLI 访问映像 ID。

isaactonyloi@DESKTOP-HV44HT6:~$ docker images -a
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    c316d5a335a5   3 weeks ago   142MB

现在我们已经获得了镜像 ID,我们可以使用 docker rmi 命令附加到镜像 ID 中删除该特定镜像,如下所示。

$ docker rmi c316d5a335a5

输出:

Untagged: nginx:latest
Untagged: nginx@sha256:2834dc507516af02784808c5f48b7cbe38b8ed5d0f4837f16e78d00deb7e7767
Deleted: sha256:c316d5a335a5cf324b0dc83b3da82d7608724769f6454f6d9a621f3ec2534a5a
Deleted: sha256:67e568696593c33b4a15c9d81dc6f67499b8d973b88eb49b53d47bf4dbf4d187
Deleted: sha256:0f8d4e3d979c540644f248b4206cf540978166b095223bdc950628dca2e8f3f1
Deleted: sha256:5d75bfe8a7422476a495b27c8a1598d1206137631d350b8bdee13bc88f365282
Deleted: sha256:8284a9e28c625b2826efdd6160ea1ff7f710881a4a2afe1ef58a5eb51d3f919e
Deleted: sha256:89a1db9e1079b7574c1a707bc8c1fe04ff723bc71d4bca8bc48653e9a32186d2
Deleted: sha256:7d0ebbe3f5d26c1b5ec4d5dbb6fe3205d7061f9735080b0162d550530328abd6

使用 Docker 中的过滤器删除未标记的本地镜像

我们还可以使用过滤器和通配符来识别悬空镜像,即不与任何容器关联的镜像。我们可以使用过滤标签 -f找到满足条件 dangling=true 的图片。

下面的命令应该列出与任何容器无关的所有镜像。但是,请注意,如果你的系统中没有未标记的镜像,则该命令将仅返回标头。

$ docker images -a -f dangling=true
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    c316d5a335a5   3 weeks ago   142MB

删除 Docker 中的所有本地镜像

当与 -a 标签一起使用时,-q 标签允许我们检索并列出本地系统中的所有镜像 ID。

$ docker images -a -q
54c9d81cbb44
c316d5a335a5

使用此命令,我们可以列出 docker rmi 下的所有镜像,以从我们的系统中删除所有镜像。这是我们如何嵌套该命令以消除所有镜像。

$ docker rmi $(docker images -a -q)

输出:

Untagged: ubuntu:latest
Untagged: ubuntu@sha256:669e010b58baf5beb2836b253c1fd5768333f0d1dbcb834f7c07a4dc93f474be
Deleted: sha256:54c9d81cbb440897908abdcaa98674db83444636c300170cfd211e40a66f704f
Deleted: sha256:36ffdceb4c77bf34325fb695e64ea447f688797f2f1e3af224c29593310578d2

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

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

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便