Photo by Muneeb Syed on Unsplash

Photo by Muneeb Syed on Unsplash

Use Linux Containers with WSL2 on Windows


Article from ADMIN 66/2021
Deploy a full Linux container environment, including a Kubernetes cluster, on Windows with Windows Subsystem for Linux version 2.

The Windows Subsystem for Linux (WSL) lets you integrate Linux commands directly into Windows Server and Linux containers and can even be used in Windows 10 Pro or Enterprise. I look at the necessary prerequisites, investigate how you can use Docker with WSL version 2 (WSL2), and even make use of Kubernetes in the process.

Linux programs and commands can be used almost seamlessly in Windows with the help of the Windows Subsystem for Linux – even in containers. Installing WSL is easy. Most distributions are free, and it can be included as quickly as it can be removed. Even Docker can be used with WSL.

Linux containers with WSL are only useful on Windows servers in the WSL2 version and currently not possible on Windows Server 2019. Windows 10 Update 2004 is the version of choice for WSL. The Long-Term Servicing Channel (LTSC) successor to Windows Server 2019, Windows Server 2022, also comes with WSL2.

Running Linux and Windows Together

After installing WSL, you can manage both Windows servers and local Windows workstations with Linux commands – a crucial point, especially for test and development environments, because containers from Windows 10 can then be transferred to production container hosts. Managing Linux servers on the network is also no problem, and even if Windows Server is installed on all systems, Windows and Linux containers can still be used in parallel.

Linux Bash can also be used to open Linux shells with Command Prompt or PowerShell. Alternatively, you can use Windows Terminal, which enables simultaneous sessions with PowerShell, Command Prompt, Azure Cloud Shell, and Linux Bash in various tabs. If you rely on containers on the network and are using Windows in parallel with WSL, you might want take a closer look at the terminal's capabilities. Companies that run their containers in parallel in Microsoft Azure benefit from additional functions. Different servers and containers in the environment can be managed in a single interface in different tabs.

The Windows Defender firewall in Windows 10 and Server 2019 can create firewall rules for each process in the Windows Subsystem for Linux and is essential if you are running containers or outgoing connections from Linux processes in containers. The outgoing processes can connect to Linux servers in the same way as to the local Windows system. WSL therefore makes sense, especially on hybrid networks where Linux and Windows are used in parallel.

Prepare WSL for Container Operation

To run Linux containers in the best possible way on Windows, you need to install WSL2. The new version uses virtualization technologies from Hyper-V, its own kernel, and a native ext4 filesystem, which makes it perfect for virtualizing containers. Likewise, Docker integration has been improved. When it comes to virtualizing Linux containers on Windows, you should always use WSL2 if at all possible. To install, run:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform

If the container host is a virtual server provided by Hyper-V, for example, you will need to enable embedded virtualization for the host. Otherwise, WSL2 will not run, affecting container host operation heavily and slowing down the containers. The containers consume more memory and cause a higher CPU load if you do not use WSL2. Despite the higher resource consumption, the containers are also fairly slow.

Before you can use WSL to run containers on a Windows virtual host, you must make some preparations. First, you need to disable dynamic memory for the virtual machine (VM) in the settings – if you have it enabled. You also need to enable virtualization extensions for the virtual CPU (vCPU), as well as MAC address spoofing. Again, you can rely on PowerShell,

Set-VMProcessor -VMName "<VM-Name>" -ExposeVirtualizationExtensions $true
Get-VMNetworkAdapter -VMName "<VM-Name>" | Set-VMNetworkAdapter-MacAddressSpoofing On

which you need to launch with elevated administrative privileges.

Configuring WSL as a Container Environment

After installing WSL for container operation, you should always check for and install any Windows updates to be on the safe side. To use the new WSL2 version, the WSL2 Linux kernel update package for x64 machines [1] must be installed on the computer. You can also run the update by typing

wsl -update

at the command line.

WSL2 is controlled and configured just like its predecessor; however, the Linux distributions support WSL1 and WSL2 differently in some cases. Each distribution can be upgraded or downgraded at any time. WSL1 and WSL2 distributions also can be run side by side. However, to run Docker containers in Windows with WSL, you will want to enable WSL2.

The wsl command-line tool lets you check and manage the WSL installation. To make sure version 2 of WSL is used, enter:

wsl --set-default-version 2

The wslconfig tool is also helpful and is used when deploying containers. The tool provides various parameters that help can perform basic administrative tasks in WSL (Table 1).

Table 1

wslconfig Paramaters

Parameter Function
/l List registered distributions.
/l /all Optionally list registered distributions, including distributions that are being installed or uninstalled.
/l /running Only count the distributions that are currently running.
/s Set a distribution as the default.
/t Terminate the distribution.
/u Unregister the distribution.

You can display a list of the current distributions and their status with the

wsl --list -verbose

command, which lets you check whether the distribution you are using for container operation is also configured for version 2. With the wsl tool, you can switch a Linux distribution retroactively (e.g., Ubuntu to WSL2):

wsl --set-version Ubuntu 2

If you see an error message when you first run the distribution, such as Error: 0x80370102 The virtual machine could not be started because a required feature is not installed , it is because Hyper-V is not available on the computer or you have not enabled embedded virtualization.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy ADMIN Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs

Support Our Work

ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.

Learn More”>


		<div class=