Coding with Titans

so breaking things happens constantly, but never on purpose

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:

  1. Quit Docker Desktop

  2. Open Command Prompt (or PowerShell)

  3. List existing WSL storages

    $ wsl --list -v
    

    Expected output:

      NAME                   STATE           VERSION
    * docker-desktop         Running         2
      docker-desktop-data    Running         2
    
  4. Turn off WSL

    $ wsl  --shutdown
    

    Output:

      NAME                   STATE           VERSION
    * docker-desktop         Stopped         2
      docker-desktop-data    Stopped         2
    
  5. Create following path (with all subfolders):

    $ mkdir D:\Docker\wsl\data\
    
  6. 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"
    
  7. Unregister the container data from WSL. It will also automatically delete the ext4.vhdx from original location.

    $ wsl --unregister docker-desktop-data
    
  8. Import the container data, but keep it into another location (i.e. on drive D: as defined above). This will automatically create ext4.vhdx file from the backup.

    $ wsl --import docker-desktop-data "D:\Docker\wsl\data" "D:\Docker\wsl\data\docker-desktop-data.tar" --version 2
    
  9. Delete the exported .tar file: D:\Docker\wsl\data\docker-desktop-data.tar and nothing more!

  10. 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!

­čî╣