全部学科
NodeJS全栈
nodejs
Python全栈
python
小程序首页
📅 2026-05-21 5 分钟 ✍️ juanwangdev

镜像查看与删除

管理本地镜像需要了解查看与删除操作,下面详细介绍。

查看镜像

docker images 命令

Bash
# 列出所有镜像
docker images

# 输出示例
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
nginx         latest    605c77e624dd   2 weeks ago     187MB
mysql         8.0       9f2349801b5e   3 weeks ago     584MB
redis         alpine    1319997363b5   1 month ago     33.2MB

关键字段:

  • REPOSITORY:镜像名称
  • TAG:版本标签
  • IMAGE ID:镜像唯一标识
  • CREATED:构建时间
  • SIZE:镜像大小

过滤镜像

Bash
# 查看指定仓库的镜像
docker images nginx

# 查看悬虚镜像(无标签)
docker images -f dangling=true

# 查看大于 100MB 的镜像
docker images -f "before=nginx:latest"

# 仅显示镜像 ID
docker images -q

查看镜像详情

Bash
# 查看镜像元数据
docker inspect nginx:latest

# 查看镜像层历史
docker history nginx:latest

# 输出示例
IMAGE          CREATED       CREATED BY                                      SIZE
605c77e624dd   2 weeks ago   CMD ["nginx" "-g" "daemon off;"]                0B
<missing>      2 weeks ago   EXPOSE map[80/tcp:{}]                           0B
<missing>      2 weeks ago   /bin/sh -c #(nop) COPY dir:                     1.2kB

删除镜像

docker rmi 命令

Bash
# 删除单个镜像
docker rmi nginx:latest

# 通过 IMAGE ID 删除
docker rmi 605c77e624dd

# 删除多个镜像
docker rmi nginx:latest mysql:8.0

# 强制删除(即使有容器在使用)
docker rmi -f nginx:latest

删除前的检查

Bash
# 检查是否有容器使用该镜像
docker ps -a --filter "ancestor=nginx:latest"

# 如果有运行中的容器,先停止并删除
docker stop <container_id>
docker rm <container_id>

# 再删除镜像
docker rmi nginx:latest

批量清理

Bash
# 删除所有悬虚镜像
docker image prune

# 删除所有未使用的镜像
docker image prune -a

# 确认提示
WARNING! This will remove all images not referenced by existing containers.
Are you sure you want to continue? [y/N] y

# 删除所有未使用的镜像和构建缓存
docker system prune -a

注意事项

  • 有容器正在使用镜像时无法直接删除(需加 -f 强制删除)
  • 删除镜像不会删除基于该镜像创建的容器
  • 同一镜像的不同标签(TAG)共享相同的层,删除一个标签不影响其他
  • 镜像删除仅删除本地副本,不影响 Registry 上的镜像

要点总结

  • docker images 查看本地镜像列表,支持按仓库、悬虚等过滤
  • docker inspect 查看镜像完整元数据,docker history 查看构建历史
  • docker rmi 删除镜像,有容器使用时需先删除容器或强制删除
  • docker image prune 清理悬虚镜像,docker system prune 全面清理
  • 删除操作不可逆,谨慎使用 -a-f 参数

📝 发现内容有误?点击此处直接编辑

← 上一篇 镜像拉取与搜索
下一篇 → 容器创建与启动
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库