A number of times, I want to create a Window Subsystem for Linux instance out of a Docker container.

The main reason for doing something like this is when a particular Linux version is not available for WSL.

The steps to follow –

Install from Docker Hub

First run the container image from the Docker Hub image

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker run -it ubuntu:22.04 /bin/bash
docker run -it ubuntu:22.04 /bin/bash
docker run -it ubuntu:22.04 /bin/bash

This runs the container and gives a prompt. Add the necessary updates / software.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
apt-get update -y
apt-get update -y
apt-get update -y

Export from Docker

Following command to be run while the docker container is running.

Get the container ID by running docker ps command, then –

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker export --output=<path to tar file>.tar <docker container id>
docker export --output=<path to tar file>.tar <docker container id>
docker export --output=<path to tar file>.tar <docker container id>

This exports the docker container to a export.tar file.

Once exported, the docker container can be stopped and removed.

Import into WSL

Import the exported tar file into WSL.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
wsl --import <instance name> <wsl save directory> <path to tar file.tar>
wsl --import <instance name> <wsl save directory> <path to tar file.tar>
wsl --import <instance name> <wsl save directory> <path to tar file.tar>