dev-machine-cleanup
Skill1 starsChanged 32 days agoApache-2.0View on GitHub
清理 macOS 本地开发机的 Docker 磁盘并抢救无响应 daemon。触发“清理开发机 / Docker 占用大 / 磁盘满了”;安全回收 cache 和 dangling 镜像,NEVER 自动删卷或有 tag 镜像。不适用于 prod、废旧代码清理、线上故障或非 Docker 个人文件。
In this repository13 other files it gives its agents.
- cleanupSkill
- code-de-rustSkill
- coding-practiceSkill
- concept-namingSkill
- drift-checkSkill
- essay-writingSkill
- harness-groomSkill
- logSkill
- makeskillSkill
- methodologySkill
- reflectSkill
- reviewSkill
- typograph-wxSkill
The file
Files
本地开发机 Docker 清理
定位
macOS 开发机的 Docker 磁盘治理。与 prod 是两个单一源:prod 走 scripts/docker-cleanup.sh(Linux + cron + 无卷风险),本地走 scripts/dev-docker-cleanup.sh。NEVER 互相搬运代码 —— prod 脚本在 macOS 上有 3 处硬失效(flock / df --output / journalctl 都不存在)和 2 处危险(builder prune --all 清掉现役层、无 dev 卷保护)。
🔴 红线(规则单一源在 rules,不在这里)
dev 数据库就住在 Docker volume 里 —— 因此 --volumes / system prune / Desktop 的 "Clean / Purge data" 一律禁用。完整 MUST/NEVER 见 .claude/rules/repository.md §dev DB 实际架构 → 推论:Air 形态下清理 Docker MUST 保护卷。
判别当前机器形态:lsof -nP -iTCP:5432 -sTCP:LISTEN —— 无 postgres 进程 = Air 形态 = 数据在卷里。
怎么做
bash scripts/dev-docker-cleanup.sh # 直接执行安全项(默认)
bash scripts/dev-docker-cleanup.sh --dry-run # 只报告不动手脚本自动做:平台守卫 → 防并发锁 → daemon 健康检测 → 卷清单快照 → build cache(两轮)→ dangling 镜像 → 一次性构建残留 → 卷零损失核对 → dev 库完整性验证 → 收支报告。
汇报时 MUST 带三个数字:宿主可用空间前后、Docker.raw 前后、卷数量核对结果。只报"清理完成"没有意义。
判读一:daemon 无响应
先分清是「没起来」还是「起不来」 —— 表现相同但处置完全不同。判据:
pgrep -f "com.docker.virtualization|dockerd"| 结果 | 含义 | 处置 |
| 有输出 | VM 在跑,daemon 启动中 | 等,别动 |
| 无输出 | VM 根本没启动 | 见下 |
VM 没启动的头号成因是磁盘写满时 VM 自我保护停机。取证:
tail -5 ~/Library/Containers/com.docker.docker/Data/log/host/com.docker.virtualization.log看到 no space left on device 紧跟 VM has stopped gracefully 即确诊。关键认知:此时 com.docker.backend 进程和 GUI 都还活着,所以 open -a Docker 无效(只是把已在跑的窗口前台化),docker info 会永久挂起而不是报错 —— 极具迷惑性。
恢复必须完整退出再冷启动,且 backend 会僵死需强杀:
osascript -e 'quit app "Docker Desktop"'kill $(pgrep -f 'Docker.app/Contents/MacOS') ; open -a DockerVM has stopped gracefully 是优雅停机不是崩溃,卷数据完好,冷启动后 dev 栈会自动恢复。
判读二:该不该删有 tag 的镜像
脚本第⑤组只报告不删,因为这需要人的判断。删的理由必须具体到能写进 commit message,凑不出理由就别删:
| 可删 | 依据 |
| 产品已下线 | 有文档背书(如 AGENTS.md 载明「旧 qingshan-saas 已下线」)+ ghcr 有远程副本可拉回 |
| 一次性验收 tag | :*-test / :b11-test 这类明确的一次性产物 |
| 已被新构建取代 | 同一服务有更新的镜像,且旧的无容器引用 |
| 过期 base 镜像 | 项目已升级到更高版本(如 node:18 → node:20+) |
| 别删 | 原因 |
近几天构建的 ghcr.io/* 与本地双 tag | 多半是待验证/待发布的生产镜像,重建一次 2G 镜像很慢 |
| 拿不准用途的 | 边际收益通常只有几 G,误删的重建成本远高于此 |
docker ps 显示镜像名时会省略 :latest —— 手工判断"这个镜像没人用"时必须同时比对两种形式,否则会把在跑的镜像当成没用的(脚本已处理,手工操作时要记得)。
判读三:Docker.raw 没缩小
Docker.raw 是稀疏文件,ls -lh 看到的表观大小(常年 926G)无意义,只看 du -sh。Desktop 4.56+ 会自动 TRIM,通常 prune 完就缩了。没缩就重启一次 Docker Desktop 触发。
NEVER 为了缩它去用 "Clean / Purge data" —— 那会连卷一起清空。
认识论:为什么元凶总是 build cache
直觉会去删镜像,但本地开发机频繁构建,build cache 无上限增长才是真凶(实测 886 条 / 113.5G,而镜像只有 38.6G)。prod 有 cron 定期清所以不显;本地一道护栏都没有,攒几个月就撑爆磁盘 —— 而磁盘撑爆会连带撂倒 Docker VM(见判读一),所以这不只是"占地方"的问题。
因此别等磁盘告警才跑。日常节奏:连续几天大量构建后、或 docker system df 的 Build Cache 超 30G 时,跑一次。
不适用
- prod 磁盘 → prod 有自己的 cron(
scripts/docker-cleanup.sh),且 AI NEVER 进 prod,命令交老猿执行 - 废旧代码 / 死导出 →
cleanupskill - 线上故障 →
incidentskill - 非 Docker 占用(照片 / 视频课程 / 微信 / Xcode 缓存)→ 本 skill 不碰。老猿要全盘地图时用
du -sh -x ~/* | sort -rh逐层下钻出占用清单交他自己下刀,AI 不代删个人数据
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

