DEV Community 👩‍💻👨‍💻

Marc Ziel
Marc Ziel

Posted on

How to reduce size of docker data volume in Docker Desktop for Windows v2

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
Enter fullscreen mode Exit fullscreen mode

The docker-desktop-data vhdx is normally in this location:

%LOCALAPPDATA%\Docker\wsl\data\ext4.vhdx
Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
zcahana profile image
Zvi Cahana • Edited on

Thanks for this useful info!

Note that both Docker Desktop and WSL2 need to be stopped before running this, otherwise an error will occur:

Optimize-VHD : Failed to compact the virtual disk.
The system failed to compact 'C:\Users\ZVICAHANA\AppData\Local\Docker\wsl\data\ext4.vhdx'.
Failed to compact the virtual disk.
The system failed to compact 'C:\Users\ZVICAHANA\AppData\Local\Docker\wsl\data\ext4.vhdx': The process cannot access
the file because it is being used by another process. (0x80070020).
At line:1 char:1
+ Optimize-VHD -Path C:\Users\ZVICAHANA\AppData\Local\Docker\wsl\data\e ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceBusy: (:) [Optimize-VHD], VirtualizationException
    + FullyQualifiedErrorId : ObjectInUse,Microsoft.Vhd.PowerShell.Cmdlets.OptimizeVhd```




Enter fullscreen mode Exit fullscreen mode

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.

Collapse
tyutyutyu profile image
István Földházi • Edited on

One-liner:

Optimize-VHD -Path $Env:LOCALAPPDATA\Docker\wsl\data\ext4.vhdx -Mode Full
Enter fullscreen mode Exit fullscreen mode

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:

net stop com.docker.service
taskkill /IM "docker.exe" /F
taskkill /IM "Docker Desktop.exe" /F
wsl --shutdown
Enter fullscreen mode Exit fullscreen mode
Collapse
danmana profile image
Dan Manastireanu

Thanks, reclaimed 14.5 GB of my ssd :D

Collapse
johnlbevan profile image
John Bevan

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

Collapse
general_markov profile image
Little Red Riding Hood

Thanks, it was really useful for me!

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.