Files
chill_notes/Docker/Docker镜像清理.md
2026-04-21 20:10:48 +08:00

1.1 KiB
Executable File
Raw Blame History

Docker 镜像清理

清理无用镜像

方法一prune 命令(推荐)

# 清理悬空镜像(无 tag 的镜像)
docker image prune -f

# 清理所有未使用镜像
docker image prune -a -f

方法二:批量清理

# 批量删除所有无用镜像,不会影响正在使用的镜像
docker images | awk 'NR!=1{print $1":"$2}' | xargs docker rmi

方法三system prune全面清理

# 清理所有未使用的容器、网络、镜像
docker system prune -f

# 清理所有未使用的镜像,不仅仅是悬空镜像
docker system prune -a -f

# 清理卷(注意:会删除数据)
docker system prune --volumes -f

清理前后对比

# 查看清理前
docker images -a

# 清理
docker system prune -f

# 查看清理后
docker images -a

定时清理(可选)

创建 cron 任务定期清理:

# 编辑 crontab
crontab -e

# 每周日凌晨 3 点清理
0 3 * * 0 /usr/bin/docker system prune -f

参考:Docker 官方文档