In this post I will demonstrate how to update the Windows Subsystem for Linux - WSL to version 2 aka WSL2. Once updated, I will demonstrate how to configure Docker to use WSL2 to run a Linux Minecraft Java Edition container natively on Windows without emulation, i.e., without a Hyper-V VM.
Pre-requirement:
Running Windows 10, updated to version 2004, Build 19041 or higher.
Virtual Machine
In case you want to install WSL 2 in a virtual machine, you need to turn off the VM and run the following command as admin on the host machine:
Set-VMProcessor -VMName Windows10 -ExposeVirtualizationExtensions $true
Notice that Windows10 is the name of my virtual machine on Hyper-V:
If you have Windows 10 build 2004 or higher, you can install WSL2 through the following command and skip steps 1-4:
wsl --install
1 - Enabling WSL
In case you don’t have the WSL installed, before installing any Linux distributions on Windows, you must enable the Windows Subsystem for Linux optional feature.
You need to restart restart Windows to apply the settings.
Check the WSL version
That session is only for those that already have WSL 1 installed. If you are installing WSL from scratch, please skip this session.
If you are using the Windows Build 19041 or higher, you can check the WSL version by opening the PowerShell command line and entering the following command:
wsl --list --verbose
So far, I am running the version one:
In case you don't have a distro Linux installed, the following message will be displayed with the address to download a Linux distro on Microsoft Store:
2 - Enable the 'Virtual Machine Platform' optional component
Before installing WSL 2, you must enable the Virtual Machine Platform optional feature.
If you prefer, it is possible to enable the feature using the following command:
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform
3 - Install your Linux distribution of choice
In case you don’t have a Linux distribution installed, you need to download install it from Microsoft Store. Follows some options available:
I am using Ubuntu 20.04 LTS:
Before launching the Linux distro, we need to update WSL to version 2. In the next session it will be demonstrated how to install WSL2.
4- Installing WSL2
WSL2 is now available in the initial release of Windows 10, version 2004 and Windows Insiders slow ring. But for now, to enable WSL2 you need to manually install the Linux kernel. It is a temporary solution and, in a few months, WSL2 will be automatically updated just like regular updates on your machine.
Use the following direct link to download the installer for the Linux kernel update package:
https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
The link is available at: https://docs.microsoft.com/en-us/windows/wsl/wsl2-kernel
Run the update setup to install WSL2:
The installation process is straightforward and you only need to follows the default options. The following UI will be displayed after successfully updated:
5 - Finish the Ubuntu installation
This session applies only for those that are installing Ubuntu or other Linux distro for the first time. Now that WSL 2 Kernel update is installed, you can open the Ubuntu App to complete the settings:
6 - Set your distribution version to WSL 2
Please skip this session in case you are installing WSL for the first time. This step is only necessary for those that already were using Ubuntu. You can now open Ubuntu to finish the setup.
Now that you have installed WSL2, it is necessary to run the following PowerShell command to update WSL to version 2:
wsl --set-version Ubuntu-20.04 2
NOTE: Ubuntu-20.04 is the name of the Linux distribution that I am using. You can run wsl -l -v to list the Linux distributions installed.
Otherwise, all you have to do is to launch Ubuntu to finish the settings, like define user and password. WSL 2 will be automatically set.
7 - Set WSL 2 as your default version
Please skip this session in case you are installing WSL for the first time. This step is only necessary for those that already were using Ubuntu. You can now open Ubuntu to finish the setup.
You need to run the following command in PowerShell to set WSL 2 as the default version in case you want to install others Linux distribution in the future:
wsl --set-default-version 2
If you run again the following command you will see that WSL2 was successfully installed:
wsl -l -v
In the next session I will configure Docker to use WSL2 for Linux containers on Windows 10.
8 - Enable WSL2 on Docker
Now that we have installed WSL2, we can configure it on Docker Desktop for Windows.
Windows Subsystem for Linux (WSL) 2 introduces a significant architectural change as it is a full Linux kernel built by Microsoft, allowing Linux containers to run natively without emulation.
Docker Desktop uses the dynamic memory allocation feature in WSL 2 to greatly improve the resource consumption. This means, Docker Desktop only uses the required amount of CPU and memory resources it needs, while enabling CPU and memory-intensive tasks such as building a container to run much faster.
Additionally, with WSL 2, the time required to start a Docker daemon after a cold start is significantly faster. It takes less than 10 seconds to start the Docker daemon when compared to almost a minute in the previous version of Docker Desktop.
More details at:
Docker Desktop WSL 2 backend
https://docs.docker.com/docker-for-windows/wsl/
If you are running a supported system, Docker will prompt you to enable WS2 during initialization.
It is possible to enable WSL2 in the Settings > General options:
Now that we have Docker configured to run Linux containers using the WSL2 engine, we can test it with a Minecraft Linux container.
9 - Minecraft Linux Container
To demonstrate the use of Docker with WSL2, I will use the itzg/docker-minecraft-server Docker Linux image that provides a Minecraft Java Edition Server.
You can use the following command to download and run the latest Minecraft Server image:
docker run -e EULA=TRUE -d -p 25565:25565 --name mc itzg/minecraft-server
Observe that with WSL2, the container initialization was only 10 seconds:
I hope you liked!
Follows some references:
Windows Subsystem for Linux Installation Guide for Windows 10
https://docs.microsoft.com/en-us/windows/wsl/install-win10
Updating the WSL 2 Linux kernel
https://docs.microsoft.com/en-us/windows/wsl/wsl2-kernel
Get started with Docker for Windows
https://docs.docker.com/docker-for-windows/
Docker Desktop WSL 2 backend
https://docs.docker.com/docker-for-windows/wsl/
Updated Dec 01, 2020
Version 7.0luisdem
Microsoft
Joined December 28, 2018
Modern Work App Consult Blog
Follow this blog board to get notified when there's new activity