Podman 教程

Podman 删除本地镜像

随着 Podman 使用时间增长,本地会积累大量不再使用的镜像,占用宝贵的磁盘空间。此时,我们可以使用 podman rmi 命令删除无用的本地镜像。

及时清理无用的镜像有如下好处:

(1)释放磁盘空间:通常 Podman 镜像通常体积庞大,尤其是包含完整操作系统的基础镜像,一个镜像可能占用 1GB 磁盘空间。

(2)清理过时或废弃的镜像:如项目迭代后,旧版本的镜像不再需要;测试环境中临时拉取的镜像;拉取失败或损坏的镜像文件,都应该及时清理,避免占用磁盘空间。

(3)避免混淆和管理混乱:如果本地镜像列表过于庞大,会导致难以快速找到需要的镜像、容易误使用旧版本的镜像、podman images 输出结果冗长,影响效率等等

基础语法

下面是 podman rmi 命令的基础格式:

podman rmi [选项] <镜像ID/镜像名称[:标签]> [镜像ID/名称...]

常用选项:

  • -f, --force --force    强制删除镜像,即使它被容器使用,例如:podman rmi -f nginx:latest

  • -i / --ignore    如果指定的镜像不存在,则忽略错误,例如:podman rmi --ignore non-existent-image

  • -a, --all    删除所有未被容器使用的镜像(注意:此选项在较新版本中已被 podman image prune 替代)

点击查看 podman rmi 命令手册

  

常用示例

(1)通过名称删除指定镜像,例如:

# 删除名为 ubuntu:22.04 的镜像
podman rmi ubuntu:22.04

# 示例
C:\Users\Administrator> podman rmi ubuntu:22.04
Untagged: docker.io/library/ubuntu:22.04
Deleted: 9fa3e2b5204f4fd5ae0d53dee5c367ac686a8a39685d9261b9d3d3c8a9cc8917

(2)通过 ID 删除指定镜像,例如:

podman rmi 35c43ace9216

# 示例
C:\Users\Administrator> podman rmi 1c390e3bb5cb
Untagged: docker.io/library/redis:latest
Deleted: 1c390e3bb5cb9c724e3947b45e57c8d73f0afe2b308141261c5669d7d97a452e

(3)强制删除被容器使用的镜像,例如:

podman rmi -f nginx:latest

# 示例
# 启动一个容器
C:\Users\Administrator> podman run -d --name my-nginx -p 8080:80 nginx
8fa300648b3fb11a95ae005192333112a3973d270c1734dbc50cc8820113f1b2

# 查看容器状态
C:\Users\Administrator> podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED        STATUS        PORTS                 NAMES
8fa300648b3f  docker.io/library/nginx:latest  nginx -g daemon o...  5 seconds ago  Up 6 seconds  0.0.0.0:8080->80/tcp  my-nginx

# 尝试删除镜像,镜像有容器正在运行
# 删除失败
C:\Users\Administrator> podman rmi nginx
Error: image used by 8fa300648b3fb11a95ae005192333112a3973d270c1734dbc50cc8820113f1b2: image is in use by a container: consider listing external containers and force-removing image

# 将容器停止
C:\Users\Administrator> podman stop my-nginx
my-nginx

# 继续尝试删除镜像,镜像有容器正在运行
# 删除失败
C:\Users\Administrator> podman rmi nginx
Error: image used by 8fa300648b3fb11a95ae005192333112a3973d270c1734dbc50cc8820113f1b2: image is in use by a container: consider listing external containers and force-removing image

# 使用 -f 选项强制删除
# 删除成功
C:\Users\Administrator> podman rmi -f nginx
Untagged: docker.io/library/nginx:latest
Deleted: 60adc2e137e757418d4d771822fa3b3f5d3b4ad58ef2385d200c9ee78375b6d5

(4)同时删除多个镜像,例如:

podman rmi ubuntu:22.04 alpine:latest 35c43ace9216

# 示例
# 查看本地拥有的镜像,存在三个镜像
C:\Users\Administrator> podman images
REPOSITORY                TAG         IMAGE ID      CREATED       SIZE
docker.io/library/redis   latest      1c390e3bb5cb  29 hours ago  142 MB
docker.io/library/mysql   latest      f6b0ca07d79d  4 weeks ago   956 MB
docker.io/library/ubuntu  latest      c3a134f2ace4  4 weeks ago   80.6 MB

# 一次性将 redis、mysql 和 ubuntu 三个镜像删除掉
C:\Users\Administrator> podman rmi redis mysql ubuntu
Untagged: docker.io/library/redis:latest
Untagged: docker.io/library/mysql:latest
Untagged: docker.io/library/ubuntu:latest
Deleted: 1c390e3bb5cb9c724e3947b45e57c8d73f0afe2b308141261c5669d7d97a452e
Deleted: f6b0ca07d79d7d19c8da64558c3ccdd4ea635ac2193f551a1cb5370f33b494e8
Deleted: c3a134f2ace4f6d480733efcfef27c60ea8ed48be1cd36f2c17ec0729775b2c8

(5)删除镜像时,忽略不存在的镜像时抛出的错误信息,例如:

podman rmi --ignore ubuntu:22.04 non-existent-image:latest

# 示例
# 查看本地镜像,没有镜像
C:\Users\Administrator> podman images
REPOSITORY  TAG         IMAGE ID    CREATED     SIZE

# 试着删除 redis 镜像,抛出了镜像不存在错误信息
C:\Users\Administrator> podman rmi redis
Error: redis: image not known

# 使用 --ignore 选项忽略错误信息
C:\Users\Administrator> podman rmi --ignore redis

C:\Users\Administrator>

更多信息请参考官方文档。

  

说说我的看法
全部评论(
没有评论
关于
本网站专注于 Java、数据库(MySQL、Oracle)、Linux、软件架构及大数据等多领域技术知识分享。涵盖丰富的原创与精选技术文章,助力技术传播与交流。无论是技术新手渴望入门,还是资深开发者寻求进阶,这里都能为您提供深度见解与实用经验,让复杂编码变得轻松易懂,携手共赴技术提升新高度。如有侵权,请来信告知:hxstrive@outlook.com
其他应用
公众号