Site icon d3v.one

Creating a Docker image from a WSL instance

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

wsl --list
Debian (Standard)
Alpine
Ubuntu

then you can export it to a file with

wsl --export Alpine alpine.tar

or, with a present installation 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 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 tar, tgz, or tar.gz file:

docker import alpine.tar alpine:wsl

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

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 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:

docker run -it alpine:wsl

As a next step, I pushed the image to my Docker repository:

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

You can view the listing of this example image by visiting the URL

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

and you can pull and run this example image by running the following command:

docker run -it d3vone/alpine:wsl

Where to go from here

Command Reference for WSL

Documentation for the Docker import command

Exit mobile version