Docker Container Cleaner
What This Does
A CLI tool that helps clean up Docker resources to free up disk space. It can:
- - List and remove stopped containers
- Remove dangling images (images with no tag)
- Remove unused images (not used by any container)
- Remove unused volumes
- Remove unused networks
- Perform a "prune all" operation (Docker system prune)
The tool provides a safe, interactive mode by default, showing what will be removed and asking for confirmation before deleting anything.
When To Use
- - Your Docker disk usage is growing and you need to free up space
- You have many stopped containers that are no longer needed
- You have old, unused images taking up disk space
- You want to clean up Docker resources in a controlled, safe way
- You need to automate Docker cleanup in scripts or CI/CD pipelines
Usage
Interactive cleanup (recommended for first use):
CODEBLOCK0
Remove stopped containers only:
CODEBLOCK1
Remove dangling images only:
CODEBLOCK2
Remove unused images (all images not used by containers):
CODEBLOCK3
Remove unused volumes:
CODEBLOCK4
Remove unused networks:
CODEBLOCK5
Force cleanup (no confirmation):
CODEBLOCK6
Dry run (show what would be removed):
CODEBLOCK7
Examples
Example 1: Interactive cleanup
CODEBLOCK8
Output:
CODEBLOCK9
Example 2: Script-friendly JSON output
CODEBLOCK10
Output:
CODEBLOCK11
Requirements
- - Python 3.x
- Docker: Must be installed and the Docker daemon must be running
- Docker CLI: Must be available in PATH (
docker command) - Docker SDK for Python: Optional, but recommended for better performance
Install Docker SDK for Python (optional):
CODEBLOCK12
Limitations
- - This is a CLI tool, not an auto-integration plugin
- Requires Docker daemon to be running and accessible
- Some operations require elevated permissions (sudo)
- Cannot clean up resources in use by running containers
- Image size calculations are approximate
- Network and volume cleanup may fail if resources are in use
- Does not clean up Docker build cache (use
docker builder prune) - Does not clean up Docker Compose resources automatically
- Performance depends on number of Docker resources
- Large cleanup operations may take significant time
Docker 容器清理工具
功能说明
一款帮助清理 Docker 资源以释放磁盘空间的命令行工具。它可以:
- - 列出并移除已停止的容器
- 移除悬空镜像(无标签的镜像)
- 移除未使用的镜像(未被任何容器使用的镜像)
- 移除未使用的卷
- 移除未使用的网络
- 执行全部清理操作(Docker 系统清理)
该工具默认提供安全的交互式模式,会显示将要删除的内容,并在删除前请求确认。
使用场景
- - Docker 磁盘使用量不断增长,需要释放空间
- 存在大量不再需要的已停止容器
- 有旧的、未使用的镜像占用磁盘空间
- 希望以可控、安全的方式清理 Docker 资源
- 需要在脚本或 CI/CD 流水线中自动化 Docker 清理
使用方法
交互式清理(首次使用推荐):
bash
python3 scripts/main.py clean
仅移除已停止的容器:
bash
python3 scripts/main.py clean --containers
仅移除悬空镜像:
bash
python3 scripts/main.py clean --images --dangling
移除未使用的镜像(所有未被容器使用的镜像):
bash
python3 scripts/main.py clean --images --unused
移除未使用的卷:
bash
python3 scripts/main.py clean --volumes
移除未使用的网络:
bash
python3 scripts/main.py clean --networks
强制清理(无需确认):
bash
python3 scripts/main.py clean --all --force
预演运行(显示将要删除的内容):
bash
python3 scripts/main.py clean --all --dry-run
示例
示例 1:交互式清理
bash
python3 scripts/main.py clean
输出:
Docker 清理工具
===================
发现以下资源:
- - 已停止的容器:3 个(占用 1.2GB)
- 悬空镜像:5 个(占用 850MB)
- 未使用的镜像:2 个(占用 450MB)
- 未使用的卷:1 个(占用 100MB)
- 未使用的网络:0 个
可释放的磁盘空间总计:2.6GB
您想要清理什么?
- 1. 移除已停止的容器
- 移除悬空镜像
- 移除未使用的镜像
- 移除未使用的卷
- 移除未使用的网络
- 以上全部
- 取消
请输入选项 [1-7]:2
即将移除 5 个悬空镜像(850MB):
- - python:3.9-alpine(悬空)
- node:16-slim(悬空)
- ...
是否确认?(y/N):y
正在移除镜像...
✅ 清理完成!释放了 850MB 磁盘空间。
示例 2:脚本友好的 JSON 输出
bash
python3 scripts/main.py status --format json
输出:
json
{
containers: {
running: 2,
stopped: 3,
stoppedsizemb: 1200
},
images: {
total: 15,
dangling: 5,
danglingsizemb: 850,
unused: 2,
unusedsizemb: 450
},
volumes: {
total: 4,
unused: 1,
unusedsizemb: 100
},
networks: {
total: 3,
unused: 0
},
totalreclaimablemb: 2600
}
系统要求
- - Python 3.x
- Docker:必须已安装且 Docker 守护进程正在运行
- Docker CLI:必须在 PATH 环境变量中可用(docker 命令)
- Docker SDK for Python:可选,但推荐使用以获得更好的性能
安装 Docker SDK for Python(可选):
bash
pip install docker
局限性
- - 这是一个命令行工具,而非自动集成插件
- 需要 Docker 守护进程正在运行且可访问
- 某些操作需要提升权限(sudo)
- 无法清理正在运行的容器所使用的资源
- 镜像大小计算为近似值
- 如果资源正在使用中,网络和卷的清理可能会失败
- 不会清理 Docker 构建缓存(请使用 docker builder prune)
- 不会自动清理 Docker Compose 资源
- 性能取决于 Docker 资源的数量
- 大型清理操作可能需要较长时间