# Docker 镜像清理 ## 清理无用镜像 ### 方法一:prune 命令(推荐) ```bash # 清理悬空镜像(无 tag 的镜像) docker image prune -f # 清理所有未使用镜像 docker image prune -a -f ``` ### 方法二:批量清理 ```bash # 批量删除所有无用镜像,不会影响正在使用的镜像 docker images | awk 'NR!=1{print $1":"$2}' | xargs docker rmi ``` ### 方法三:system prune(全面清理) ```bash # 清理所有未使用的容器、网络、镜像 docker system prune -f # 清理所有未使用的镜像,不仅仅是悬空镜像 docker system prune -a -f # 清理卷(注意:会删除数据) docker system prune --volumes -f ``` ## 清理前后对比 ```bash # 查看清理前 docker images -a # 清理 docker system prune -f # 查看清理后 docker images -a ``` ## 定时清理(可选) 创建 cron 任务定期清理: ```bash # 编辑 crontab crontab -e # 每周日凌晨 3 点清理 0 3 * * 0 /usr/bin/docker system prune -f ``` --- > 参考:[Docker 官方文档](https://docs.docker.com/config/pruning/)