How to move WSL distributions, including Docker images, to new locations on Windows

March 15, 2023 MrAnyx 4 min de lecture

If you need to move your WSL distributions, including Docker images, to a new location due to limited storage or for performance and security reasons, you've come to the right place. Indeed, by default, wsl distribution and located in your main storage driver which is the C:/ drive.

Moving WSL distributions can be a complex task, but in this blog post, we'll provide you with a comprehensive guide to help you move your Linux environment seamlessly.

By following these step-by-step instructions, you'll be able to transfer your WSL distributions to a new location without losing any data or configuration. Keep reading to learn how to move your WSL distributions and improve your computer's storage and performance!

Determine the distribution you want to move

First of all, before exporting the data from the WSL distributions, we need to know the name of the distributions.

To do this, nothing could be simpler, you just have to execute the following command:

wsl --list --verbose

Or more quickly

wsl -l -v

This command should return the following result:

In the NAME column, you will find the names of all the distributions that are currently installed on your computer.

Note that in my case, there are also the two distributions related to Docker Desktop. This is normal. If you use WSL as a base for Docker Desktop, you should also see the two lines that relate to docker-desktop and docker-desktop-data.

The commands that we will run next can also be used to change the localization of the two Docker Desktop distributions.

Export the distribution’s data

Now that we know the names of the different WSL distributions, we can export their content in order to keep the data they contain.

To do this, simply run the following command:

wsl --export <distro_name> <distro_name>.tar

by replacing <distro_name> with the name of the distribution you want.

You should end up with something like this:

wsl --export docker-desktop docker-desktop.tar

Once executed, you should see a new .tar file appear at the location where you are in your terminal.

Unregister the distribution

Now we can delete the distribution we have just exported. Indeed, all the data contained in the distribution are present in the tar file that we have just generated.

To delete a distribution, type the following command:

wsl --unregister <distro_name>

Again, you will only need to replace <distro_name> with the distribution you wish to remove.

For example, this is equivalent to issuing the following command:

wsl --unregister docker-desktop

This command will remove the distribution in question and free up the associated space. Thus, since distributions are installed by default on the C:/ drive, this will greatly reduce the amount of space used on that drive.

Import the data to a new location

All that remains is to re-import the distribution to a new location. To do this, run the following command:

wsl --import <distro_name> <path> <distro_name>.tar --version 2

This command will recreate in the desired location the <distro_name> distribution from the backup we made earlier with the .tar file.

You should end up with something like this:

wsl --import docker-desktop D:\WSL\docker-desktop docker-desktop.tar --version 2

If you want to recreate the distribution in a sub-folder that does not yet exist, you may need to create this sub-folder first.

If you are using Git Bash as your terminal, the path entered should look something like this: /d/WSL/path.

Conclusion

Wrapping up, if you're looking for a way to free up space on your computer or want to give your system a performance boost, moving your WSL distributions, including Docker images, to a new location on Windows can be a great solution.

Even if you're not a tech expert, don't worry, it's not as complicated as it may seem. Just follow our simple guide, which includes exporting your distribution's data, unregistering the distribution, and then importing the data to a new location. By following these steps, you can relocate your Linux environment without losing any important data or configuration. So why not give it a try and see the difference it can make to your system?


Cette œuvre est mise à disposition selon les termes de la licence Licence Creative Commons