Moving Docker and WSL (Ubuntu) Data to Another Drive

If you are a gamer like me you probably will need all that sweet, sweet, space in your C drive for all the fun and games, and this guide provides detailed instructions to move Docker and WSL (Ubuntu) data from the C: drive to the D: drive using symbolic links, ensuring compatibility with existing configurations.


Prerequisites

  1. Open PowerShell as Administrator:
  2. Right-click on the Start menu and select Windows PowerShell (Admin).
  3. Back up important data:
  4. Make sure to back up any critical data before proceeding.

Part 1: Moving Docker Data to D:

Step 1: Move Docker Folder to the New Drive

  1. Navigate to Docker's Current Data Location:
  2. Open File Explorer and go to C:\Users\YourUserName\AppData\Local\Docker.
  3. Move the Entire Docker Folder:
  4. Cut and paste the Docker folder to D:\Docker. After moving, the folder structure should look like: D:\Docker\wsl\data

Step 2: Recreate Docker Folder Structure on C:

  1. Open Command Prompt as Administrator:
  2. Press Win + R, type cmd, then press Ctrl + Shift + Enter.
  3. Recreate the Necessary Folder Structure:
  4. Run the following command: cmd mkdir "C:\Users\YourUserName\AppData\Local\Docker\wsl"
  1. Create the Symbolic Link:
  2. Run this command in the same Command Prompt: cmd mklink /D "C:\Users\YourUserName\AppData\Local\Docker\wsl\data" "D:\Docker\wsl\data"
  1. Check the Link:
  2. In File Explorer, navigate to C:\Users\YourUserName\AppData\Local\Docker\wsl and verify that data appears as a shortcut pointing to D:\Docker\wsl\data.

Step 5: Restart Docker

  1. Start Docker Desktop:
  2. Open Docker Desktop and ensure it starts without any errors. If successful, Docker is now using the data at D:\Docker\wsl\data.

Part 2: Moving WSL (Ubuntu) Data to D:

Step 1: Export the WSL (Ubuntu) Distribution

  1. Run the Export Command:
  2. In PowerShell (Admin), export your WSL distribution (e.g., Ubuntu) to a .tar file: powershell wsl --export Ubuntu D:\WSLBackup\Ubuntu.tar
  3. This creates a backup file named Ubuntu.tar in D:\WSLBackup.
  4. Confirm the Backup:
  5. Check that Ubuntu.tar is present in D:\WSLBackup.

Step 2: Unregister the WSL Distribution

  1. Remove the Current Installation:
  2. Run the following command in PowerShell to unregister Ubuntu: powershell wsl --unregister Ubuntu
  3. This removes Ubuntu from WSL without deleting the backup file.

Step 3: Import Ubuntu to D:

  1. Import Ubuntu Using the Backup File:
  2. Run the following command to import Ubuntu to the D: drive: powershell wsl --import Ubuntu D:\WSL\Ubuntu D:\WSLBackup\Ubuntu.tar --version 2
  3. This imports the Ubuntu distribution to D:\WSL\Ubuntu, creating a new ext4.vhdx file in that location.

Step 4: Verify the New WSL Data Location

  1. Check for ext4.vhdx:
  2. Navigate to D:\WSL\Ubuntu in File Explorer and confirm the presence of the ext4.vhdx file, which contains your WSL data.

Step 1: Recreate the Folder Structure on C:

  1. Open Command Prompt as Administrator:
  2. If not already open, press Win + R, type cmd, and press Ctrl + Shift + Enter.
  3. Recreate the Necessary Folder Structure:
  4. Run the following command: cmd mkdir "C:\Users\YourUserName\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState"
  1. Create the Symbolic Link:
  2. Run this command in Command Prompt: cmd mklink /D "C:\Users\YourUserName\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState\ext4.vhdx" "D:\WSL\Ubuntu\ext4.vhdx"
  1. Check the Link:
  2. Navigate to C:\Users\YourUserName\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState and verify that ext4.vhdx appears as a shortcut to D:\WSL\Ubuntu\ext4.vhdx.

Step 4: Start WSL and Test

  1. Run Ubuntu:
  2. Start Ubuntu using the following command in PowerShell: powershell wsl -d Ubuntu
  3. If Ubuntu starts without any issues, it is now successfully using D:\WSL\Ubuntu\ext4.vhdx for its data.

Summary

After following these steps, both Docker and WSL (Ubuntu) data have been moved to the D: drive. Symbolic links on the C: drive ensure compatibility with their original locations.