woensdag 1 februari 2023

Large vhdx Docker

 The virtual harddrive of wsl for Docker can become very large when adding en removing containers.

As well as storing data like Mssql databases.

The file is located in your Appdata dictory of Docker %UserProfile%\AppData\Local\Docker\wsl\data\ext4.vhdx.

To cleanup the drive you can purge all unused images en containers in docker, but you can also shrink the file with Powershell.

The command Optimize-Vhd is used to shrink the file.

Optimize-Vhd -Path .\ext4.vhdx -Mode full

If you get an error when using this command, it's not available, then you'll have to install Hyper-V tools via Windows features.

Before you can run the Optimize command, you'll have to quit Docker Desktop

Check if there are running containers with 

wsl -l --running or wsl -l -v to show the details

if nothing is running you need to stop wsl, with wsl --shutdown.

Now you can run the Optimize-Vhd command.


Read more:

Scott hanselman's blog


Move the vhdx file to another drive:

wsl --shutdown
wsl --export docker-desktop-data docker-desktop-data.tar
wsl --unregister docker-desktop-data
wsl --import docker-desktop-data X:\path\to X:\path\to\docker-desktop-data.tar --version 2
ref: https://github.com/docker/for-win/issues/7348