Created
July 4, 2024 20:15
-
-
Save MatMercer/c16cbc13c3caac4edc48e908e08987e5 to your computer and use it in GitHub Desktop.
Shrink docker desktop too huge virtual disk. Shrink docker desktop ext4 WSL Windows 10 11
This file contains 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
# Script to shrink docker desktop data, a common issue at WSL 2 | |
# Most of the script is generated with ChatGPT | |
# Check if the script is running as administrator | |
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent() | |
$adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator | |
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal($currentUser) | |
if (-not $currentPrincipal.IsInRole($adminRole)) { | |
Write-Host "Requesting administrator privileges..." | |
Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs | |
exit | |
} | |
# Define the VHDX file path | |
$vhdxPath = "$env:LOCALAPPDATA\Docker\wsl\disk\docker_data.vhdx" | |
# Function to stop Docker Desktop application | |
function Stop-DockerDesktop { | |
Write-Host "Stopping Docker Desktop application..." | |
# Stop Docker Desktop GUI application | |
$guiProcess = Get-WmiObject Win32_Process -Filter "Name = 'Docker Desktop.exe'" | |
if ($guiProcess) { | |
$guiProcess | ForEach-Object { | |
try { | |
Stop-Process -Id $_.ProcessId -Force | |
Write-Host "Stopped GUI process ID: $($_.ProcessId)" | |
} catch { | |
Write-Host "Failed to stop GUI process ID: $($_.ProcessId)" | |
} | |
Start-Sleep -Seconds 1 | |
} | |
} else { | |
Write-Host "Docker Desktop GUI application is not running." | |
} | |
# Stop Docker backend and dev-envs processes | |
$dockerProcesses = Get-WmiObject Win32_Process -Filter "Name = 'com.docker.backend.exe' OR Name = 'com.docker.dev-envs.exe'" | |
if ($dockerProcesses) { | |
$dockerProcesses | ForEach-Object { | |
try { | |
Stop-Process -Id $_.ProcessId -Force | |
Write-Host "Stopped process ID: $($_.ProcessId)" | |
} catch { | |
Write-Host "Failed to stop process ID: $($_.ProcessId)" | |
} | |
Start-Sleep -Seconds 1 | |
} | |
Write-Host "Docker Desktop application stopped." | |
} else { | |
Write-Host "Docker Desktop application is not running." | |
} | |
} | |
# Stop Docker Desktop application | |
Stop-DockerDesktop | |
# Stop Docker Desktop service | |
Write-Host "Stopping Docker Desktop service..." | |
Stop-Service -Name "com.docker.service" -Force | |
Start-Sleep -Seconds 1 | |
Write-Host "Docker Desktop service stopped." | |
# Stop WSL 2 container | |
Write-Host "Stopping WSL 2 container..." | |
wsl --shutdown | |
Start-Sleep -Seconds 1 | |
Write-Host "WSL 2 container stopped." | |
# Shrink the VHDX file using diskpart | |
Write-Host "Shrinking VHDX file using diskpart..." | |
$diskpartScript = @" | |
select vdisk file="$vhdxPath" | |
compact vdisk | |
"@ | |
$diskpartScriptPath = [System.IO.Path]::GetTempFileName() | |
$diskpartScript | Out-File -FilePath $diskpartScriptPath -Encoding ASCII | |
diskpart /s $diskpartScriptPath | |
Remove-Item $diskpartScriptPath | |
Write-Host "VHDX file shrink operation completed." | |
# Pause at the end of the script | |
Write-Host "Script execution completed." | |
Pause |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment