Introduction
Docker, a powerful containerization tool on Windows, has undergone significant changes with the integration of WSL2 (Windows Subsystem for Linux 2). However, configuring the storage location for Docker containers can be a challenge. By default, Docker stores containers and downloaded images on the system drive, potentially leading to limited disk space. In this comprehensive guide, we will explore an effective method to change the storage location for Docker containers using WSL2 on Windows. Follow the step-by-step instructions below to optimize storage utilization, enhance your Docker workflow, and gain better control over disk space management.
Steps to Change Docker Container Storage Location with WSL2 on Windows:
Step 1: Close Docker Desktop:
Before making any modifications, ensure that Docker Desktop is closed to prevent any conflicts during the process. By shutting down the application, you can safely proceed with changing the storage location for Docker containers.
Step 2: Launch Command Prompt or PowerShell:
To execute the necessary commands, open either Command Prompt or PowerShell on your Windows system. These command-line interfaces will provide you with the required tools to successfully modify the Docker container storage location.
Step 3: List Existing WSL Storages:
To identify the available WSL instances and their current states, execute the following command:
wsl --list -v
This command will display a list of existing WSL storages, including the Docker instances.
Expected output:
NAME STATE VERSION
* docker-desktop Running 2
docker-desktop-data Running 2
Step 4: Turn Off WSL:
To stop the running WSL instances, execute the following command:
wsl --shutdown
By turning off WSL, you ensure that there are no active processes interfering with the upcoming modifications.
Output:
NAME STATE VERSION
* docker-desktop Stopped 2
docker-desktop-data Stopped 2
Step 5: Create the New Storage Path:
To define the desired storage location for your Docker containers, create a new directory and its subfolders. For example, execute the following command to create the path “E:\Docker\wsl\data” on drive E:
mkdir E:\Docker\wsl\data\
Step 6: Export Containers and Their Data:
Export your Docker containers and associated data using the command:
wsl --export docker-desktop-data "E:\Docker\wsl\data\docker-data-export.tar"
Depending on the size of the original ext4.vhdx file, this process may take some time. Exporting the data ensures that you retain your containers and their configurations during the storage location transition.
Step 7: Unregister Container Data from WSL:
Remove the container data and delete the original ext4.vhdx file by executing the command:
wsl --unregister docker-desktop-data
This step clears the previous container data from WSL and prepares for the import process.
Step 8: Import Container Data to the New Location:
Import the container data to the new storage location using the following command:
wsl --import docker-desktop-data "E:\Docker\wsl\data" "E:\Docker\wsl\data\docker-data-export.tar" --version 2
By specifying the new location in the command, Docker will create a new ext4.vhdx file in the desired directory. This imported data will be used by Docker for future container operations.
Step 9: Delete the Exported .tar File:
After the import process is complete, delete the exported .tar file to free up disk space. Execute the command:
del "E:\Docker\wsl\data\docker-data-export.tar"
This step removes the .tar file, which is no longer necessary once the container data has been successfully imported.
Conclusion:
By following the step-by-step guide outlined above, you can efficiently change the storage location for Docker containers using WSL2 on Windows. Although a more user-friendly interface or simplified configuration settings are currently unavailable, these instructions empower you to optimize storage utilization and enjoy a streamlined Docker experience. Take control of your containerization workflow, leverage WSL2, and unlock the benefits of enhanced storage management on Windows.
Reference links:
Latest Posts:
- Unifi Dream Router: Unleashing Its Power Across Homes, Businesses, and Beyond
- Part 6: Maintaining and Troubleshooting Your Home Server
- Part 5: Setting Up and Managing Server Applications
- Part 4: Networking and Security for Home Servers
- Part 3: Selecting Your Home Server Operating System
- Part 2: Choosing the Right Hardware for Your Home Server