Environment
-
- Version: 4.29.0? (I accidentally upgraded it before writing this article.)
- Used backend: WSL2
-
Windows
- Edition: 11 Pro
- Version: 23H2
Target File Example
C:\Users\{user}\AppData\Local\Docker\wsl\data\ext4.vhdx
Background
Docker Desktop for Windows uses a virtual machine (typically virtualization technologies like WSL or Hyper-V) to run Linux containers on Windows.
The file system used within this virtual machine is generally in the ext4 format, which is commonly used in Linux.VHDX is a type of virtual hard disk format developed by Microsoft, used as a disk image for virtual machines in virtualized environments.
Docker Desktop for Windows uses this format files to provide storage for Linux containers.ext4.vhdx is a virtual hard disk in VHDX format that hosts the ext4 file system.
Docker Desktop for Windows uses this file to store data such as containers, images, and volumes that are mounted.
This file will grow as the disk usage by WSL2 (i.e. Docker Desktop for Windows) increases, but it will not automatically shrink even if the disk usage decreases.
Remove Dangling Docker Objects.
As written above, doing this does not reduce the size of ext4.vhdx
.
However, doing this makes optimization more efficient.
We simply open PowerShell and execute following commands.
-
docker image prune
-
docker builder prune
Optimizing with diskpart
Open PowerShell as admin.
The reason for admin is that UAC pops up and Command Prompt opens if we start diskpart as a normal user.-
Shut down WSL to block access from it to a virtual hard disk.
wsl --shutdown
-
Enter diskpart.
diskpart
-
Specify the virtual hard disk to be optimized.
select vdisk file="C:\Users\{user}\AppData\Local\Docker\wsl\data\ext4.vhdx"
-
Attach the specified virtual hard disk.
Since there is no need to change that data, we do it as read-only to prevent operational errors.
attach vdisk readonly
-
Optimize.
compact vdisk
-
Detach the specified virtual hard disk.
detach vdisk
-
Exit diskpart.
exit
Optimizing with Hyper-V
* The Hyper-V does not exist on Home Edition.
We need to activate Hyper-V before we optimize.
Open
Turn Windows features on or off
dialog.
We just press theWindows
key and theR
key at the same time, then inputoptionalfeatures
and clickOK
.
Now we can execute the command to optimize it.
Open PowerShell as admin.
This is because the command can only be executed by admin.-
Shut down WSL to block access from it to a virtual hard disk.
wsl --shutdown
-
Move current dir to the location where
ext4.vhdx
is located.
cd C:\Users\{user}\AppData\Local\Docker\wsl\data
-
Optimize.
Since VHDX is optimized inQuick
mode by default, we will specifyFull
mode explicitly for maximum optimization.
Optimize-VHD -Path .\ext4.vhdx -Mode Full
Top comments (1)
If you encounter some problems with
diskpart
or don't haveHyper-V
on your Windows this can be useful.