Odds and Ends

Reclaim Docker WSL Disk Space

I ran out of system SSD disk space on my Win10 PC at home and tracked a large share of the usage to a vhdx virtual disk image file in a Docker Desktop folder. Below is the summary of what I found.

Docker Desktop for Windows uses WSL to manage all your images and container files and keeps them in a private virtual hard drive (VHDX) called ext4.vhdx.

It’s usually in C:\Users\YOURNAME\AppData\Local\Docker\wsl\data and you can often reclaim some of the space if you’ve cleaned up (pruned your images, etc) with Optimize-Vhd under an administrator PowerShell shell/prompt.

(from hanselman.com)

The appropriate command to try to shrink this file is optimize-vhd -Path .\ext4.vhdx -Mode full from an Administrator PowerShell in the appropriate directory, but if your system is Win10 Home (like mine was) then you may see this error:

optimize-vhd : The term 'optimize-vhd' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ optimize-vhd -Path .\ext4.vhdx-Mode full
+ ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (optimize-vhd:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

A comment on a Github issue related to this disk usage growth provides the following workaround for Win10 Home. The procedure is basically to shutdown the WSL subsystem, and enter the diskpart prompt and compact the file from there.

PS C:\Users\YOURNAME\AppData\Local\Docker\wsl\data> wsl --shutdown
PS C:\Users\YOURNAME\AppData\Local\Docker\wsl\data> diskpart

# Microsoft DiskPart version 10.0.19041.964

# Copyright (C) Microsoft Corporation.
# On computer: YOUR-HOSTNAME

DISKPART> select vdisk file="C:\Users\YOURNAME\AppData\Local\Docker\wsl\data\ext4.vhdx"

# DiskPart successfully selected the virtual disk file.

DISKPART> attach vdisk readonly

#   100 percent completed

# DiskPart successfully attached the virtual disk file.

DISKPART> compact vdisk

#   100 percent completed

# DiskPart successfully compacted the virtual disk file.

DISKPART> detach vdisk

# DiskPart successfully detached the virtual disk file.

DISKPART> exit

# Leaving DiskPart...

Sources

Tags: disk docker wsl windows