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 (8)

Collapse
 
zcahana profile image
Zvi Cahana • Edited

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

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
 
getoxs profile image
Mateusz

If you get error:

Optimize-VHD : Hyper-V encountered an error trying to access an object on computer 'K48689' because the object was not
found. The object might have been deleted. Verify that the Virtual Machine Management service on the computer is runnin
g.
Enter fullscreen mode Exit fullscreen mode

Run command:

mofcomp %SYSTEMROOT%\System32\WindowsVirtualization.V2.mof
Enter fullscreen mode Exit fullscreen mode
Collapse
 
danmana profile image
Dan Manastireanu

Thanks, reclaimed 14.5 GB of my ssd :D

Collapse
 
jayarjo profile image
Barbakadze • Edited

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.

Collapse
 
vmpartner profile image
Musin Vitaly

For me help wsl --shutdown

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!