prune
使用该命令时,需确保客户端与守护程序的API版本均不低于1.25。可通过在客户端执行docker version命令来验证当前版本是否符合要求。
docker volume prune [OPTIONS]
此操作旨在清除那些当前没有任何容器正在引用的本地数据卷。
OPTIONS
| 名称,简写 | 说明 |
| --filter | 提供过滤值。 |
| --force , -f | 不提示确认信息,直接删除。 |
rm
调用该命令的前提是客户端和守护程序的API版本均需达到1.21及以上。用户可在客户端运行docker version命令以确认版本信息。
docker volume rm [OPTIONS] VOLUME [VOLUME...]
该功能用于移除一个或多个指定的数据卷。自1.25版本开始,引入了--force或-f参数,允许用户在不进行确认提示的情况下强制删除卷。
补充说明:Docker提供了移除未使用资源的功能,涵盖镜像、容器、卷和网络等对象。
具体参考docker prune相关文档。
系统提供prune命令,专门用于清理不再被引用的镜像、容器、卷及网络资源。
Prune images
docker image prune命令用于移除那些既没有标签也没有被任何容器引用的镜像,这类镜像通常被称为dangling(悬挂)镜像。
示例1:docker image prune
执行后,名为redis且无标签也无引用的镜像将被删除。
#docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ae2feff98a0c 4 days ago 133MB
redis ef47f3b6dc11 8 days ago 104MB
centos latest 300e315adb2f 12 days ago 209MB
ubuntu latest f643c72bc252 3 weeks ago 72.9MB
docs/docker.github.io latest 32ed84d97e30 6 months ago 1GB
# docker image prune
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ae2feff98a0c 4 days ago 133MB
centos latest 300e315adb2f 12 days ago 209MB
ubuntu latest f643c72bc252 3 weeks ago 72.9MB
docs/docker.github.io latest 32ed84d97e30 6 months ago 1GB
示例2:移除所有没有容器使用的镜像 -a
docker image prune -a
若希望跳过警告提示直接执行,可使用--force或-f参数。
docker image prune -f
示例3:执行过滤删除:
例如仅处理创建时间超过24小时的镜像。
docker image prune -a --filter "until=24h"
关于更多过滤器的用法,请参阅docker image prune的手册页。
移除容器s
默认情况下,停止运行的容器不会自动删除,除非在启动时使用了--rm参数。由于停止容器的可写层仍占用磁盘空间,建议使用docker container prune命令进行清理。
其他相关参数的用法类似于docker images prune。
移除卷
卷可能被一个或多个容器挂载并使用,并占用主机存储空间。出于数据安全考虑,卷不会被自动移除,以防止数据意外丢失。
docker volume prune
其他相关参数的用法类似于docker images prune。
移除网络
虽然Docker网络本身不占用磁盘空间,但它们会生成iptables规则、桥接网络服务及路由条目。清理未被容器使用的网络有助于保持系统整洁。
docker network prune
其他相关参数的用法类似于docker images prune。
移除 Everything
docker system prune命令是一个快捷方式,用于一次性移除镜像、容器和网络等资源。
在Docker 17.06.0及更早版本中,卷也会被一并移除。但在Docker 17.06.1或更高版本中,必须显式指定--volumes参数才能移除卷。
示例(没有移除卷):
# docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all build cache
Are you sure you want to continue? [y/N] y
示例(有了移除卷功能):添加--volumes
# docker system prune --volumes
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all volumes not used by at least one container
- all dangling images
- all build cache
Are you sure you want to continue? [y/N] y
其他相关参数的用法类似于docker images prune。
以上为个人经验总结,希望能给大家一个参考,也希望大家多多支持本站。如有错误或未考虑完全的地方,望不吝赐教。