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

63 lines
1.1 KiB
Markdown
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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/)