GPU-accelerated Docker containers in Windows using WSL2
By: Jim Vekemans
Tags: Ubuntu, Gpu, Docker, Windows 10, Machine Learning

What is WSL?

The Windows Subsystem for Linux, or WSL for short, lets developers run a GNU/Linux environment — including most command-line tools, utilities, and applications — directly on Windows, unmodified, without the overhead of a traditional virtual machine or dual boot setup.

You can:

Choose your favourite GNU/Linux distributions from the Microsoft Store.

Run Bash shell scripts and GNU/Linux command-line applications including:

Tools: vim, emacs, tmux

Languages: NodeJS, JavaScript, Python, Ruby, C/C++, C# & F#, Rust, Go, etc.

Services: SSHD, MySQL, Apache, MongoDB, PostgreSQL.

Install additional software using your own GNU/Linux distribution package manager.

Invoke Windows applications using a Unix-like command-line shell.

Invoke GNU/Linux applications on Windows.

What is different in WSL 2?

WSL 2 is a new version of the Windows Subsystem for Linux architecture that powers the Windows Subsystem for Linux to run ELF64 Linux binaries on Windows. Its primary goals are to increase file system performance, as well as adding full system call compatibility.

This new architecture changes how these Linux binaries interact with Windows and your computer’s hardware, but still provides the same user experience as in WSL 1 (the current widely available version).

Individual Linux distributions can be run with either the WSL 1 or WSL 2 architecture. Each distribution can be upgraded or downgraded at any time and you can run WSL 1 and WSL 2 distributions side by side. WSL 2 uses an entirely new architecture that benefits from running a real Linux kernel.

How to install WSL and configure WSL2

* To install WSL, you need to be on Windows 10/11 version 2004 or newer (Build 19041 or newer). Check which version you have with WIN+R and starting winver. You should see something like this:

Open a PowerShell prompt as administrator and enter the following commands:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Restart your system to enable WSL and download the WSL kernel update. Run the updater as administrator and just click next until the installer in finished.

If there is an error telling you that WSL needs to be enabled first, you probably skipped the part where you rebooted your computer. If that doesn’t help, look for “Windows Features” in your Windows settings and manually check the “Windows Subsystem for Linux” checkbox.

Reboot again if you had to do these steps and run the kernel updater after login.

You should see this if the installation was successful:

You just enabled WSL!

Changing WSL version to 2

The default installation is still for the original WSL, but we want WSL2 because it provides far better performance. This article shows the performance difference between WSL, WSL2 and Ubuntu 20.04 LTS. To set the WSL version to 2, open PowerShell as administrator again and paste this command:

wsl --set-default-version 2

Configuring WSL2 system resources

By default WSL has access to all available cores and threads in your system as well as half of the available amount of system memory. For example a 4 core / 8 thread laptop with 16 GB of memory will provide WSL with 8 threads and 8 GB of memory. You can change these values to whatever values you prefer by creating a “.wslconfig” file. This file needs to be placed in the root directory of your Windows user like this: C:\Users\<yourUserName>\.wslconfig . Here's what a possible .wslconfig file would look like:

[wsl2]

memory=4GB # Limits memory in WSL2 to 4 GB

processors=3 # Makes WSL2 use up to 3 threads

The full list of possible configurations can be found on https://docs.microsoft.com/en-us/windows/wsl/wsl-config.

Install Ubuntu from the Microsoft Store

Open the Microsoft Store and look for Ubuntu. You should pick the latest LTS release to install.

Other versions are supported by Docker as well but if you want to enable GPU acceleration, pick Ubuntu 20.04 LTS. Ubuntu 18.04 LTS and Debian have been tested but failed to use the GPU to render applications and instead used software rendering.

After installation is complete, you should be able to launch the application which will ask you for a UNIX username and password. You can pick these however you like, they don’t affect your Windows login. Just be sure to remember your password.

If you can launch Ubuntu and get this screen, you can proceed to installing Docker.

Docker Desktop configuration

Download and install Docker Desktop. After installation reboot your computer and start Docker. Configure your settings to match the following: (You can choose to start and open on login if you want to, doing so will consume system resources even when not using Docker or WSL actively):