# Docker 自动清理脚本 # 用途:定期清理 Docker 数据,防止 VHDX 文件过大 # 使用:手动运行或设置为计划任务 Write-Host "=== Docker 清理开始 ===" -ForegroundColor Cyan Write-Host "时间: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')`n" -ForegroundColor Yellow # 1. 显示清理前的空间使用 Write-Host "清理前的空间使用:" -ForegroundColor Yellow docker system df Write-Host "" # 2. 清理停止的容器 Write-Host "清理停止的容器..." -ForegroundColor Yellow $containers = docker container prune -f Write-Host $containers -ForegroundColor Gray # 3. 清理未使用的镜像 Write-Host "`n清理未使用的镜像..." -ForegroundColor Yellow $images = docker image prune -a -f Write-Host $images -ForegroundColor Gray # 4. 清理未使用的卷 Write-Host "`n清理未使用的卷..." -ForegroundColor Yellow $volumes = docker volume prune -f Write-Host $volumes -ForegroundColor Gray # 5. 清理构建缓存 Write-Host "`n清理构建缓存..." -ForegroundColor Yellow $cache = docker builder prune -a -f Write-Host $cache -ForegroundColor Gray # 6. 显示清理后的空间使用 Write-Host "`n清理后的空间使用:" -ForegroundColor Green docker system df # 7. 检查 VHDX 文件大小 Write-Host "`n检查 VHDX 文件大小..." -ForegroundColor Yellow $vhdxPath = "D:\Users\Mr_Wu01\Documents\DockerDesktopWSL\disk\docker_data.vhdx" if (Test-Path $vhdxPath) { $vhdxSize = [math]::Round((Get-Item $vhdxPath).Length/1GB, 2) if ($vhdxSize -gt 50) { Write-Host "⚠️ VHDX 文件较大: $vhdxSize GB" -ForegroundColor Red Write-Host "建议运行压缩脚本: .\scripts\docker-compress-vhdx.ps1" -ForegroundColor Yellow } elseif ($vhdxSize -gt 20) { Write-Host "⚠️ VHDX 文件: $vhdxSize GB (建议关注)" -ForegroundColor Yellow } else { Write-Host "✅ VHDX 文件: $vhdxSize GB (正常)" -ForegroundColor Green } } else { Write-Host "❌ 未找到 VHDX 文件" -ForegroundColor Red } Write-Host "`n=== 清理完成 ===" -ForegroundColor Green Write-Host "节省的空间请查看上方统计信息" -ForegroundColor Cyan