Ok, this is a short one. In this context, WSL means, of course, the “Windows Subsystem for Linux”, the compatibility layer which allows running 64 bit Linux binary executables natively on Windows 10 and Windows Server 2019 in a console window. This is a Windows feature, present since the Windows 10 Fall Creators Update from 2017. You can download different Linux distributions as Apps from the Microsoft Store, e.g. Ubuntu, Debian, and Alpine. Every App is a running Linux installation, though without a real Linux kernel or GUI applications.
Every WSL distribution contains a full file system and you can export all files to a tar file. First, check the name of the distribution
then you can export it to a file with
or, with a present installation of gzip (e.g. from here), a lot of space can be saved with
Please note, that the export command was introduced with Windows 10 version 1903 (the “April 2019 Update”) and does not work in older versions.
I use Alpine in this example because it’s by far the smallest WSL distribution.
If you have ever wondered how to create a snapshot of a WSL state, this is the way to do it. Copying all files of a WSL installation in the host file system won’t work, because all copy or compression tools are missing special file system features that are necessary for running a WSL instance. But you can save and restore the state by exporting and importing to and from a
tar file. Ideally, this is done at least once directly after installation, so the installation state can be restored.
The next step, creating a Docker image, is as easy as importing the
Or almost, because we need to create at least a minimum of additional Dockerfile instructions, a
CMD to run the image interactively and an environment would be no bad idea either
This command creates a Docker image with less than 9 MB in size. See the Documentation for the docker import command for a list of supported Dockerfile instructions. As a CMD argument,
/bin/sh is used here because Alpine WSL does not contain an installation of the Bash out of the box. Now we can test and run the image:
As a next step, I pushed the image to my Docker repository:
You can view the listing of this example image by visiting the URL
and you can pull and run this example image by running the following command: