Docker Desktop for Windows v2, which uses WSL2, stores all image and container files in a separate virtual volume (vhdx). This virtual hard disk file can automatically grow when it needs more space (to a certain limit). Unfortunately, if you reclaim some space, i.e. by removing unused images, vhdx doesn't shrink automatically. Luckily, you can reduce its size manually by calling this command in PowerShell (as Administrator):
Optimize-VHD -Path c:\path\to\data.vhdx -Mode Full
The docker-desktop-data vhdx is normally in this location:
%LOCALAPPDATA%\Docker\wsl\data\ext4.vhdx
Top comments (8)
Thanks for this useful info!
Note that both Docker Desktop and WSL2 need to be stopped before running this, otherwise an error will occur:
To stop Docker Desktop, right click its tray icon and choose "Quit Docker Desktop".
To stop WSL2, open a command prompt and run
wsl --shutdown
.One-liner:
If the above command fails with "The process cannot access the file because it is being used by another process.", stop services and tasks using that file:
If you get error:
Run command:
Thanks, reclaimed 14.5 GB of my ssd :D
I get an error:
Optimize-VHD : 'Local\Docker\data\ext4.vhdx4' is not an existing virtual hard disk file.
Although vhdx4 file is right there.
For me help wsl --shutdown
Thanks for sharing this.
For anyone who doesn't have this cmdlet / the Hyper-V module, you can add it via Add/Remove Programs, Windows Features, per: superuser.com/a/1307442/156700
Thanks, it was really useful for me!