Created
September 23, 2025 08:29
-
-
Save dbsanfte/8de69c17491653162940ba6732b93342 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### Run this script on Windows (outside of the devcontainer!) to clear out Docker data and shrink the Docker VHD. | |
| # Check if running as administrator, if not, relaunch as admin | |
| if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { | |
| Write-Host "Script requires administrator privileges. Relaunching as administrator..." -ForegroundColor Yellow | |
| # Get the current script path | |
| $scriptPath = $MyInvocation.MyCommand.Path | |
| # Relaunch the script as administrator | |
| Start-Process PowerShell -Verb RunAs -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$scriptPath`"" | |
| # Exit the current non-elevated session | |
| exit | |
| } | |
| Write-Host "Running with administrator privileges. Starting Docker cleanup and VHD shrink process..." -ForegroundColor Green | |
| try { | |
| # Detect user home directory | |
| $userHome = $env:USERPROFILE | |
| Write-Host "Detected user home directory: $userHome" -ForegroundColor Cyan | |
| # Step 1: Docker system prune (silently continue if Docker isn't running) | |
| Write-Host "Step 1: Running Docker system prune..." -ForegroundColor Yellow | |
| # Check if Docker is available and running | |
| $dockerAvailable = $false | |
| try { | |
| docker version | Out-Null | |
| $dockerAvailable = $true | |
| } catch { | |
| Write-Host "Docker is not running or not available. Skipping system prune..." -ForegroundColor Cyan | |
| } | |
| if ($dockerAvailable) { | |
| docker system prune -af --volumes | |
| if ($LASTEXITCODE -eq 0) { | |
| Write-Host "Docker system prune completed successfully." -ForegroundColor Green | |
| } else { | |
| Write-Host "Docker system prune failed, but continuing..." -ForegroundColor Yellow | |
| } | |
| } | |
| # Step 1.5: Additional cleanup in docker-desktop WSL distribution | |
| Write-Host "Step 1.5: Cleaning up Docker volumes in WSL..." -ForegroundColor Yellow | |
| try { | |
| wsl -d docker-desktop -e sh -c "rm -rf /tmp/docker-desktop-root/var/lib/docker/volumes/*" | |
| if ($LASTEXITCODE -eq 0) { | |
| Write-Host "WSL Docker volumes cleanup completed successfully." -ForegroundColor Green | |
| } else { | |
| Write-Host "WSL Docker volumes cleanup failed, but continuing..." -ForegroundColor Yellow | |
| } | |
| } catch { | |
| Write-Host "Failed to execute cleanup in docker-desktop WSL distribution: $($_.Exception.Message)" -ForegroundColor Yellow | |
| Write-Host "Continuing with the rest of the process..." -ForegroundColor Cyan | |
| } | |
| # Step 2: Stop Docker services and processes | |
| Write-Host "Step 2: Stopping Docker Desktop services and processes..." -ForegroundColor Yellow | |
| # Stop Docker service | |
| Stop-Service -Name "com.docker.service" -Force -ErrorAction SilentlyContinue | |
| # Stop Docker Desktop application | |
| Get-Process "Docker Desktop" -ErrorAction SilentlyContinue | Stop-Process -Force | |
| # Stop Docker backend processes | |
| Get-Process "com.docker.backend" -ErrorAction SilentlyContinue | Stop-Process -Force | |
| Get-Process "com.docker.build" -ErrorAction SilentlyContinue | Stop-Process -Force | |
| Write-Host "Docker processes stopped." -ForegroundColor Green | |
| # Wait a moment for services to fully stop | |
| Start-Sleep -Seconds 10 | |
| # Step 3: Shutdown WSL | |
| Write-Host "Step 3: Shutting down WSL..." -ForegroundColor Yellow | |
| wsl --shutdown | |
| if ($LASTEXITCODE -ne 0) { | |
| throw "WSL shutdown failed" | |
| } | |
| # Wait for WSL to fully shutdown | |
| Start-Sleep -Seconds 5 | |
| # Step 4: Shrink Docker VHD using diskpart | |
| Write-Host "Step 4: Shrinking Docker VHD..." -ForegroundColor Yellow | |
| # Build VHD path using detected user home directory | |
| $vhdPath = Join-Path $userHome "AppData\Local\Docker\wsl\disk\docker_data.vhdx" | |
| Write-Host "Docker VHD path: $vhdPath" -ForegroundColor Cyan | |
| if (-not (Test-Path $vhdPath)) { | |
| throw "Docker VHD file not found at: $vhdPath" | |
| } | |
| $diskpartCommands = @" | |
| select vdisk file="$vhdPath" | |
| attach vdisk readonly | |
| compact vdisk | |
| detach vdisk | |
| exit | |
| "@ | |
| $diskpartCommands | diskpart | |
| if ($LASTEXITCODE -ne 0) { | |
| throw "Diskpart operation failed" | |
| } | |
| Write-Host "VHD compaction completed successfully." -ForegroundColor Green | |
| # Step 5: Restart WSL (it will start automatically when needed) | |
| Write-Host "Step 5: WSL will restart automatically when Docker starts..." -ForegroundColor Yellow | |
| # Step 6: Start Docker service | |
| Write-Host "Step 6: Starting Docker Desktop service..." -ForegroundColor Yellow | |
| Start-Service -Name "com.docker.service" | |
| # Start Docker Desktop application | |
| Start-Process "C:\Program Files\Docker\Docker\Docker Desktop.exe" | |
| Write-Host "Docker cleanup and VHD shrink process completed successfully!" -ForegroundColor Green | |
| Write-Host "Docker Desktop is starting up. Please wait a moment for it to fully initialize." -ForegroundColor Cyan | |
| } catch { | |
| Write-Host "Error occurred: $($_.Exception.Message)" -ForegroundColor Red | |
| Write-Host "You may need to manually restart Docker Desktop." -ForegroundColor Yellow | |
| exit 1 | |
| } finally { | |
| # Keep the window open so user can see the results | |
| Write-Host "Press any key to close..." -ForegroundColor Gray | |
| $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment