Moving WSL2 Docker Engine data from one server to another

I have a Windows 11 Workstation with Docker Engine installed there with WSL2 backend.
It has been using it’s own ext4.vhdx for sometime and has reached the Virtual size: 2929 GB (almost 3 TB). After removing all useless data and shrinking the file to Physical size: 539 GB I want to put all the Docker data (containers and volumes) from HDD with 4TB to HDD with 1TB.
Now there is a question: how can I do it? Looking into official MS documentation about WSL2 disk spaces I see that there is an easy way of extending the Virtual size but there is no official way of decreasing/shrinking/compacting it.
I wonder what if I just stop Docker and move the ext4.vhdx from one Docker instance to a new one installed on 1 TB partition? Will it work? Or do I need to make any additional steps to decrease the Virtual size in this ext4.vhdx file prior copying to a new partition?
I understand that it should work if I copy this file to a disk with physical size more than virtual
size but if it less?

And the main question here is that when I do an extension of a virtual size I also do it in WSL2 Linux system to allow system know about the new size like this sudo resize2fs /dev/sde 400G
Extending can be done online (with mounted disks) since Linux kernel allows it

But how to decrease it (if it’s needed)? If I quit Docker Desktop it stops its services and I see no volumes available to modify the size. If I start Docker I can’t do anything since all is locked
e.g.

maxim@pc0152:/mnt/c/Windows/system32$ df -h | grep isocache
/dev/sde        2.0T  2.4G  1.9T   1% /mnt/wsl/docker-desktop-data/isocache
maxim@pc0152:/mnt/c/Windows/system32$ sudo resize2fs /dev/sde 1024G
[sudo] password for maxim:
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/sde is mounted on /mnt/wsl/docker-desktop-data/isocache; on-line resizing required
resize2fs: On-line shrinking not supported
maxim@pc0152:/mnt/c/Windows/system32$ sudo umount -f /dev/sde
umount: /mnt/wsl/docker-desktop-data/isocache: target is busy.

Maybe I use a wrong direction here? Maybe someone can point me to a doc where I can read about how to move all Docker Engine data to a new disk with the physical size that is lower than virtual size of ext4 file and lower than appropriate file system in this ext4 file?

Run a docker system prune -a to remove all unnessary images and shut down containers, then shrink your vhdx according this or this.

  • export distro as tar: wsl --export docker-desktop-data ddfw_export.tar
  • create a backup of your vhdx if you want
  • unregister distro: wsl --unregister docker-desktop-data
  • import distro from tar: wsl --import docker-desktop-data E:\docker-destop\ ddfw_export.tar

You don’t need to decrease the virtual size.

Thanks, I’ll try
But you say that I do no need to do anything with this in WSL
/dev/sde 2.2T 2.6G 2.1T 1% /mnt/wsl/docker-desktop-data/isocache
Is it ok that the size of this device is more than the HDD on which it will be placed(1TB)?

I am no WSL expert, I can only say what I observe and experience.

All of my WSL distributions have a bigger virtual disk size than the physical size of their vhdx file. On my systems the partition size of the physical disk is way smaller than the virtual disk size of the WSL distributions.

Your virtual disk size is 2.2T, but you only use 2.6G. df rounds this up to 1% usage. The physical disk space in the vhdx file is not pre-allocated, it is expanded when necessary. So yes, if your WSL distribution fills up the virtual disk space, which expands the vhdx, it will be a problem if it exceeds the physically available disk space as it will affect not only the WSL distribution, but also the Windows host.

I have been in this situation exactly once: I had plenty of typescript projects inside a wsl distro and their node_modules folders clogged up the virtual harddisk. I exported this distro, unregistered it and imported it again from another drive.

Thanks, seems like it works pretty well! I have only one question left in my mind.

Looking in this manual I see that WSL 2 automatically resizes these VHD files to meet storage needs. By default each VHD file used by WSL 2 is initially allocated a 1TB maximum amount of disk space
Is there a way to set up WSL the way it will create not 1 TB maximum amount of disk space but less?
E.g. I have a disk with one partition with 1TB. Some part of the disk is allocated by Windows, Program Files etc so in reality we have about 900 GB free on the partition. Can I import tar file from previous docker-desktop-data WSL distro which is 350 GB with maximum virtual size 650 GB or it’s not possible at all? I see that WSL2 size can be modified but can’t see if it’s possible do decrease the default 1 TB virtual size. Maybу there is no point in decreasing the virtual size since it will give an error in any case: either something overfills the created virtual size of the file or the linux/docker will start throwing errors if vhdx file tries to allocate more space that is left on device?

I see that I can create (not import) a file with any virtual size
New-VHD -Path c:\Users\user\AppData\Local\Docker\wsl\data\ext4_2.vhdx -SizeBytes 736GB
and it will show Virtual size: 736 GB. Interesting if I can use it for docker and if yes what is the way of importing all info from a tar file?

And while the question has not been answered I have faced another real problem
I was able to --export and --unregister the distribution
But when I tried to make an import
wsl --import docker-desktop-data c:\Users\user\AppData\Local\Docker\wsl\data\ C:\Work\ddfw_export.tar
I received

Unspecified error

I tried several times but had the same error.
Some technical details
Docker Desktop 4.12.0 version
ddfw_export.tar is 292 366 581 760 bytes
import fails when a newly created ext4.vhdx grows up to 263 879 680 KB
And as I found the issue has been idle since 2019

I have tried several steps to fix from this link above (ran as admin, import not tar but previously backed ext4.vhdx file, wsl --set-default-version 2 - nothing helped). I’ll try to use LxRunOffline but I’m pretty sure that this won’t help

Any ideas?