Ok, this is a short one. In this con­text, WSL means, of course, the “Win­dows Sub­sys­tem for Linux”, the com­pat­i­bil­ity layer which al­lows run­ning 64 bit Linux bi­nary ex­e­cuta­bles na­tively on Win­dows 10 and Win­dows Server 2019 in a con­sole win­dow. This is a Win­dows fea­ture, present since the Win­dows 10 Fall Cre­ators Up­date from 2017. You can down­load dif­fer­ent Linux dis­tri­b­u­tions as Apps from the Mi­crosoft Store, e.g. Ubuntu, De­bian, and Alpine. Every App is a run­ning Linux in­stal­la­tion, though with­out a real Linux ker­nel or GUI ap­pli­ca­tions.

Every WSL dis­tri­b­u­tion con­tains a full file sys­tem and you can ex­port all files to a tar file. First, check the name of the dis­tri­b­u­tion

wsl --list
Debian (Standard)
Alpine
Ubuntu

then you can ex­port it to a file with

wsl --export Alpine alpine.tar

or, with a present in­stal­la­tion of gzip (e.g. from here), a lot of space can be saved with

wsl --export Alpine alpine.tar && gzip -9 alpine.tar

Please note, that the ex­port com­mand was in­tro­duced with Win­dows 10 ver­sion 1903 (the “April 2019 Up­date”) and does not work in older ver­sions.

I use Alpine in this ex­am­ple be­cause it’s by far the small­est WSL dis­tri­b­u­tion.

If you have ever won­dered how to cre­ate a snap­shot of a WSL state, this is the way to do it. Copy­ing all files of a WSL in­stal­la­tion in the host file sys­tem won’t work, be­cause all copy or com­pres­sion tools are miss­ing spe­cial file sys­tem fea­tures that are nec­es­sary for run­ning a WSL in­stance. But you can save and re­store the state by ex­port­ing and im­port­ing to and from a tar file. Ide­ally, this is done at least once di­rectly after in­stal­la­tion, so the in­stal­la­tion state can be re­stored.

The next step, cre­at­ing a Docker image, is as easy as im­port­ing the tar , tgz , or tar.gz file:

docker import alpine.tar alpine:wsl

Or al­most, be­cause we need to cre­ate at least a min­i­mum of ad­di­tional Dock­er­file in­struc­tions, a CMD to run the image in­ter­ac­tively and an en­vi­ron­ment would be no bad idea ei­ther

docker import -c "CMD /bin/sh" -c "ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" alpine.tar alpine:wsl

This com­mand cre­ates a Docker image with less than 9 MB in size. See the Doc­u­men­ta­tion for the docker im­port com­mand for a list of sup­ported Dock­er­file in­struc­tions. As a CMD ar­gu­ment, /bin/sh is used here be­cause Alpine WSL does not con­tain an in­stal­la­tion of the Bash out of the box. Now we can test and run the image:

docker run -it alpine:wsl

As a next step, I pushed the image to my Docker repos­i­tory:

docker tag alpine:wsl d3vone/alpine:wsl
docker login -u d3vone
docker push d3vone/alpine:wsl

You can view the list­ing of this ex­am­ple image by vis­it­ing the URL

https://hub.docker.com/r/d3vone/alpine

and you can pull and run this ex­am­ple image by run­ning the fol­low­ing com­mand:

docker run -it d3vone/alpine:wsl

Where to go from here

Com­mand Ref­er­ence for WSL

Doc­u­men­ta­tion for the Docker im­port com­mand