Windows Subsystem for Linux 2 with Docker Desktop

A step-by-step guide to setting up Windows Subsystem for Linux 2 and integrating Docker Desktop

Charlotte Crabb
Apolitical Engineering
5 min readMar 1, 2021

--

When I joined Apolitical, it was my first data science role — I couldn’t wait to get started. A month or so in and I was pretty comfortable working in my Windows dev environment, quietly pleased that I had finally conquered my inexplicable fear of the command line.

Despite this, there was a recurring theme causing me problems: not having Linux. In particular, in a Windows environment I was unable to easily spin up Docker containers, which we use to deploy code to our production environment. The solution? Windows Subsystem for Linux (WSL) integrated with Docker Desktop.

This blog post covers installation and setup of WSL 2, followed by how to integrate it with Docker Desktop.

Windows Subsystem For Linux

WSL enables you to run a Linux environment directly on a Windows operating system, so you can take advantage of Linux command-line tools, utilities and applications. At the time of writing, WSL 2 is the most recent version of WSL.

Docker Desktop

Docker Desktop is a piece of software that allows you to build containerised applications and services on your own computer. For production services that run in Docker containers, this is great for testing, since it means your development environment is very similar to the production environment.

Installing WSL 2

Step 1: Check prerequisites

The prerequisite for installation is Windows 10 with version 1903 or higher. You can check your Windows version by entering ver in Windows Command Prompt.

Step 2: Enable WSL and virtualisation

WSL and the Virtual Machine Platform are optional features on Windows. To enable them, first open PowerShell as Administrator. To do this, press the Windows Logo key to enter the search field in the taskbar, then type ‘PowerShell’, right click on the Windows PowerShell app and select ‘Run as administrator’.

Opening Windows PowerShell as administrator

In PowerShell, run the following command to enable WSL:

Next, run this second command to enable the Virtual Machine Platform:

After running both of these, restart your computer.

Step 3: Download the Linux kernel update package

This step ensures you have the latest features installed for WSL. You can download the Linux kernel update package from the WSL installation instructions here. After downloading, run the package by double clicking on it, and select ‘yes’ to allow permissions.

Step 4: Set WSL 2 as default

To use WSL 2 as the default version of WSL, open PowerShell again and run:

Step 5: Install a Linux distribution

You can install a Linux distribution from the Microsoft Store. Open the Microsoft Store, search ‘Linux’ and select a distribution of your choice. On the distribution’s page, click ‘Get’ to install. On our team, we use Ubuntu. The most recent version of Ubuntu in the Microsoft Store at the time of writing is shown below.

Ubuntu 20.04 LTS in the Microsoft Store

Step 6: Launch your Linux distribution

The first time you launch your Linux distribution, it will ask you to create a username and password. Enter these to complete the setup of WSL 2.

Note: It’s possible you may receive an error when you first launch your Linux distribution. It might look something like this:

An Ubuntu console window showing an error on first launch

In this case, check the error code (above, it’s 0x80370102) and refer to the handy troubleshooting section in the WSL installation guide. On some computers you will need to enable virtualisation within the computer’s BIOS. How to do this varies with different computers and there are plenty of guides for this online, such as this one.

Installing Docker Desktop

Now that we have WSL 2 set up, we can install Docker Desktop and integrate it with WSL 2.

Step 1: Download Docker Desktop

Download Docker Desktop for Windows from the Docker website. Run the downloaded file and follow the installation instructions.

Step 2: Configure Docker to use WSL 2

Once installed, start Docker Desktop and navigate to Settings > General. Check the box next to ‘Use WSL2 based engine’. Click ‘Apply & Restart’.

Selecting ‘Use WSL2 based engine’ in the General Settings tab

Step 3: Integrate Docker with your Linux distribution

Once Docker Desktop restarts, navigate to Settings > Resources > WSL Integration. Check the box next to ‘Enable integration with my default WSL distro’ and select any distributions on which you wish to integrate Docker with WSL. Click ‘Apply & Restart’ to finish the integration of Docker Desktop with WSL.

Enabling integration with the default WSL distro in the Resources Settings tab

A Note on Memory

You might find that you encounter ‘out of memory’ errors when using Docker with WSL 2. This could be because your Linux distribution has a limit on the amount of your computer’s RAM it can access. One way to resolve these issues is to create a swap file that will allow your Linux distribution to access additional RAM. You can find instructions for adding swap space here.

That’s it — this completes the setup of WSL 2 integrated with Docker Desktop. Happy developing!

Useful Links:

--

--