HowTo: Change Docker containers storage location with WSL2 on Windows 10
Once I started playing with Docker on Windows it quickly turned out that latest version heavily rely on WSL 2, in comparison to an older Hyper-V based approach. One thing that changed significantly during this technology transition was lack of a setting screen to actually define the location (and other params), where the containers and downloaded images should be stored. As the space they occupy grows really fast and default is not always the best place for it!
I don’t understand why, but because of such practices (i.e. most of developer tools blindly assuming only drive C:
exists inside the PC and is also infinite storage at the same time), the free space on a system drive runs extremely fast towards 0MB available. I would be very happy, if there would have been any management console tool, configuration file or at least a guide on how to reconfigure it to utilize drive D:
instead. There is a Microsoft documentation, which describes a WSL configuration file, but it seems to be a bit old these days. At the same time I couldn’t find anything useful on Docker website itself.
What I finally spot was this tricky solution from Franks Chow on StackOverflow.
Let me quote all required steps here too:
-
Quit Docker Desktop
-
Open Command Prompt (or PowerShell)
-
List existing
WSL
storages$ wsl --list -v
Expected output:
NAME STATE VERSION * docker-desktop Running 2 docker-desktop-data Running 2
-
Turn off
WSL
$ wsl --shutdown
Output:
NAME STATE VERSION * docker-desktop Stopped 2 docker-desktop-data Stopped 2
-
Create following path (with all subfolders):
$ mkdir D:\Docker\wsl\data\
-
Export containers and their data. This step can actually take some time depending on the size of the original
ext4.vhdx
file.$ wsl --export docker-desktop-data "D:\Docker\wsl\data\docker-desktop-data.tar"
-
Unregister the container data from
WSL
. It will also automatically delete theext4.vhdx
from original location.$ wsl --unregister docker-desktop-data
-
Import the container data, but keep it into another location (i.e. on drive
D:
as defined above). This will automatically createext4.vhdx
file from the backup.$ wsl --import docker-desktop-data "D:\Docker\wsl\data" "D:\Docker\wsl\data\docker-desktop-data.tar" --version 2
-
Delete the exported .tar file:
D:\Docker\wsl\data\docker-desktop-data.tar
and nothing more! -
Start Docker Desktop and run your containers.
Final thoughts
This whole approach seems to be a little hackish. I am surprised it was the only working solution. Is there anything actually blocking or preventing Docker Team or Microsoft to simplify this setting and providing it as nice UI anywhere or as single command at least?
Great it’s achievable and done!
PS C:> wsl -l Debian (默认) PS C:> wsl --list -v NAME STATE VERSION docker-desktop-data Running 2 docker-desktop Running 2 PS C:> wsl --list -v * Debian Running 2 docker-desktop-data Stopped 2 PS C:> wsl --list -v NAME STATE VERSION * Debian Running 2 docker-desktop-data Stopped 2 docker-desktop Running 2 NAME STATE VERSION * Debian Running 2 docker-desktop-data Stopped 2 docker-desktop Running 2 PS C:> wsl --shutdown PS C:> wsl --list -v * Debian Stopped 2 docker-desktop-data Stopped 2 docker-desktop Stopped 2 PS C:> wsl --export Debian "D:\Users\[username]\AppData\Local\Docker\wsl\data\debian.tar" PS C:> wsl --export docker-desktop "D:\Users\[username]\AppData\Local\Docker\wsl\data\docker-desktop.tar" PS C:\Users\[username]> wsl --export docker-desktop-data "D:\Users\[username]\AppData\Local\Docker\wsl\data\docker-desktop-data.tar" PS C:\Users\[username]> wsl --unregister Debian 正在注销... PS C:\Users\[username]> wsl --unregister docker-desktop-data 正在注销... PS C:\Users\[username]> wsl --unregister docker-desktop 正在注销... PS C:\Users\[username]> wsl --import Debian "D:\Users\[username]\AppData\Local\Docker\wsl\data" D:\Users\[username]\AppData\Local\Docker\wsl\data\debian.tar --version 2 PS C:\Users\[username]> wsl --import docker-desktop "D:\Users\[username]\AppData\Local\Docker\wsl\data" D:\Users\[username]\AppData\Local\Docker\wsl\data\docker-desktop.tar --version 2 提供的安装位置已在使用中。 PS C:\Users\[username]> wsl --import docker-desktop-data "D:\Users\[username]\AppData\Local\Docker\wsl\data" D:\Users\[username]\AppData\Local\Docker\wsl\data\docker-desktop-data.tar --version 2 提供的安装位置已在使用中。 PS C:\Users\[username]> wsl --list -v NAME STATE VERSION * Debian Stopped 2 PS C:\Users\[username]> wsl --import docker-desktop-data "D:\Users\[username]\AppData\Local\Docker\wsl\data" "D:\Users\[username]\AppData\Local\Docker\wsl\data\docker-desktop-data.tar" --version 2 提供的安装位置已在使用中。 PS C:\Users\[username]> wsl --list -v NAME STATE VERSION * Debian Running 2 docker-desktop-data Running 2 docker-desktop Running 2 PS C:\Users\[username]>