# Migrating WSL and Docker to Another Disk

> This article is currently an experimental machine translation and may contain errors. If anything is unclear, please refer to [original Chinese version](<https://blog.wendain.ing/posts/move-docker-and-wsl/>). I am continuously working to improve the translation.

When using Docker Desktop + WSL 2 on Windows, the C drive can easily fill up. The usual culprits are two kinds of data:

- WSL data, such as Ubuntu's `ext4.vhdx`
- Docker's images, containers, and volume data, which are typically managed by Docker Desktop's WSL backend

> [!TIP]
>
> **What is `ext4.vhdx`?**
>
> A WSL distribution, such as Ubuntu, is essentially a Linux system registered inside Windows; most of its Linux filesystem content is stored in a virtual disk file, usually `ext4.vhdx`.

Below is how to migrate these.

## Preparation

### Prepare the target directories for migration

I plan to migrate to the following locations respectively:

```text
D:\wsl      for Ubuntu
D:\docker   for Docker images, containers, volumes, and other data
```

You can specify your own directories.
### Check the WSL status

Run

```powershell
wsl -l -v
```

**Make sure it is WSL2, otherwise the following steps do not apply**.

The output on my machine is

```powershell
  NAME              STATE           VERSION
* Ubuntu            Stopped         2
  docker-desktop    Stopped         2
```

We will migrate Ubuntu later. **If the Linux distribution you use is not Ubuntu, simply replace `Ubuntu` with the name shown in the `NAME` column**.

### Shut down WSL and Docker

Run

```powershell
wsl --shutdown
```

and close Docker Desktop.

## Migrating WSL

First, export Ubuntu:

```powershell
wsl --export Ubuntu D:\wsl\Ubuntu.vhdx --format vhd
```

This step may take a while.

Verify that the export succeeded:

```powershell
Get-Item D:\wsl\Ubuntu.vhdx
```

After confirming success, unregister the original Ubuntu:

```powershell
wsl --unregister Ubuntu
```

Then organize the directory:

```powershell
New-Item -ItemType Directory -Force D:\wsl\Ubuntu
Move-Item D:\wsl\Ubuntu.vhdx D:\wsl\Ubuntu\ext4.vhdx
```

Re-import it:

```powershell
wsl --import-in-place Ubuntu D:\wsl\Ubuntu\ext4.vhdx
```

Check whether it succeeded:

```powershell
wsl -l -v
wsl -d Ubuntu
```

If you can enter Ubuntu, the migration was successful.

Here, if you launch WSL separately from Windows Terminal, you may find that the previous configuration no longer works:

```bash
不存在具有所提供名称的分发。
错误代码: Wsl/Service/CreateInstance/ReadDistroConfig/WSL_E_DISTRO_NOT_FOUND
```

Solution: change the content of the "Command line" field shown in the image to

```powershell
C:\WINDOWS\system32\wsl.exe -d Ubuntu --cd ~
```

Then save and reopen it.

<img src="https://image.wendaining.top/image-20260514154427805.png" style="zoom:33%;" />

## Migrating Docker

You can migrate directly using Docker Desktop's GUI.

Open Docker Desktop, go to Settings -> Resources -> Advanced, modify the Settings -> Resources -> Advanced options as shown in the image, change them to the directory location you set, and then click Apply & Restart.

<img src="https://image.wendaining.top/image-20260514154541018.png" style="zoom:33%;" />
