Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stephensmitchell/f263bf9acb1dce0e2fe88f22a10751d5 to your computer and use it in GitHub Desktop.
Save stephensmitchell/f263bf9acb1dce0e2fe88f22a10751d5 to your computer and use it in GitHub Desktop.
Backup and restore WSL2 vhdx, avoid 256GB vhdx limit for docker desktop WSL2 native

Determine the expected size of the docker-desktop-data volume

  • Get the base image size + additional + all docker volumes using docker system df
  • Add raw uncompressed size for alpine (50MB), docker for your version of windows (700MB) and linux (80MB)

docker system df

Get info for the docker-desktop-data VHD

PS C:\Users\windows-admin\AppData\Local\Docker\wsl\data> Get-VHD -Path C:\Users\windows-admin\AppData\Local\Docker\wsl\data\ext4.vhdx


ComputerName            : WINDOWS10PRO
Path                    : C:\Users\windows-admin\AppData\Local\Docker\wsl\data\ext4.vhdx
VhdFormat               : VHDX
VhdType                 : Dynamic
FileSize                : 115GB <in bits>
Size                    : 256GB <in bits>
MinimumSize             :
LogicalSectorSize       : 512
PhysicalSectorSize      : 4096
BlockSize               : 1048576
ParentPath              :
DiskIdentifier          : A9****
FragmentationPercentage : 10
Alignment               : 1
Attached                : False
DiskNumber              :
IsPMEMCompatible        : False
AddressAbstractionType  : None
Number                  :

Quit docker in windows or via

taskkill /F /IM "Docker Desktop.exe"

Stop the docker service

net stop com.docker.service

Shutdown wsl

wsl -shutdown

Create a backup of the docker-data virtual disk. -VHDType Fixed will set the size to fixed volume the max size of the volume, the size attribute returned by Get-VHD. Ensure you have enough HD space to proceed, you can use an external volume but it will be slow.

# PS>
Convert-VHD -Path C:\Users\windows-admin\AppData\Local\Docker\wsl\data\ext4.vhdx -DestinationPath C:\Users\windows-admin\AppData\Local\Docker\wsl\data\ext4_fixed.vhdx -VHDType Fixed

Compact the fixed disk... the size will not change

# PS>
Optimize-VHD -Path C:\Users\windows-admin\AppData\Local\Docker\wsl\data\ext4_fixed.vhdx 

Delete the dynamic disk

# PS>
Remove-Item -Path C:\Users\windows-admin\AppData\Local\Docker\wsl\data\ext4.vhdx 

Rename the fixed disk to ext4.vhdx

# PS>
Rename-Item -Path C:\Users\windows-admin\AppData\Local\Docker\wsl\data\ext4_fixed.vhdx -NewName ext4.vhdx 

Export the docker-desktop the docker-desktop-data wsl distros. This should bring their .vhdx with them, and only bring the data from the in-use portion of the volume.

wsl --export docker-desktop E:\docker-desktop.tar # 57MB
wsl --export docker-desktop-data E:\docker-desktop-data.tar # 115GB

Optionally compress the distro tars to save filespace and backup elsewhere

bash # can also use 7zip via windows
tar cvzf docker-desktop.tar docker-desktop.tar.gz # 17MB
tar cvzf docker-desktop-data.tar docker-desktop-data.tar.gz # 13GB

Import the docker-desktop-data wsl distros

# wsl.exe --import <DistributionName> <InstallLocation> <FileName>
wsl --import docker-desktop-data %APPDATA%/Docker

Move in either direction to restore or backup the image. Convert to larger than 256GB fixed volume to surpass WSL limits.

Start docker

  • Can use .exe it wil restart the docker service
  • Docker pre-configured in wsl2 mode will also start wsl
  • Pull or run a large amount of stuff without fearing the native wsl2 256GB limit that cannot be reconfigured using the Microsoft method because docker appears to have left docker-desktop-data 'shell-less'
  • Monitor that you do not broach the >256GB limit, much like you did using WSL1. When reach <your size in GB above 256GB>, you may not be able to start or recover your data. There is no built-in altitude warning.
  • If you reach the <your size in GB above 256GB> limit and cannot start docker, you can attempt to export the docker-desktop-data wsl distro and run it natively with some linux filesystem utilities and hyper-v .vhdx drivers, it may be possible to prune the image manually then remount it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment