Before you go, check out these stories!

0
Hackernoon logoHow To Run Docker Linux Containers Natively on Windows by@avinashsingh

How To Run Docker Linux Containers Natively on Windows

With Docker Desktop running on WSL 2, users can leverage Linux workspaces and avoid having to maintain both Linux and Windows build scripts.

How is it different?

Applications that run on docker are limited to applications that are natively supported by the host operating system.

In other words, Docker for Windows can only host Windows applications inside Docker containers, and Docker on Linux supports only Linux apps.

Docker on Windows: Challenges

Docker on windows always has been a challenge, earlier when I first used Docker back in 2017 it had following limitations,

It had a strict requirement of Windows versions that are supported, several containers were not available for windows platform. Support for the orchestration systems like Kubernates and Mesos was not complete.

Most of this was related to the fact that Docker was originally written and built for Linux.

There were a few workarounds to make it work on WSL (Windows Subsystem for Linux) but they were complicated and not complete.

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.

Starting with WSL2, Docker can run in it’s full flow in Windows and you can use images built for Linux.

Below tutorial will help you install Docker on your WSL in windows.

Prerequisites

Before you install the Docker Desktop WSL 2 backend, you must complete the following steps:

Install Windows 10, version 2004 or higher (Build 19041 or higher).

At the moment of writing this article, to update to Windows 10 version 2004 (Build 19041), you will need to join the Windows Insider program and select the “Release Preview” ring. The public release should arrive by late May.

Enable WSL 2 feature on Windows.

Open PowerShell as Administrator and run:

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

Enable the ‘Virtual Machine Platform’ optional component

Open PowerShell as Administrator and run:

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

Restart your machine at this point to complete the WSL install and update to WSL 2.

Install the linux kernel package required to update the WSL version to WSL 2.

Details of kernel are in below link,

https://docs.microsoft.com/en-us/windows/wsl/wsl2-kernel

Installer is at,

https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi

Set WSL 2 as your default version

Open PowerShell as Administrator and run:

wsl --set-default-version 2

Install your Linux distribution of choice

Open the Microsoft Store and select your favorite Linux distribution.

Some of the popular one’s are below :

The first time you launch a newly installed Linux distribution, a console window will open and you’ll be asked to wait for a minute or two for files to de-compress and be stored on your PC. All future launches should take less than a second.

You will then need to create a user account and password for your new Linux distribution.

Check for the list of linux distributions,

wsl -l -v

Set the distribution to use WSL 2

wsl --set-version <distribution name> <versionNumber>

Installing Docker Desktop

Download Docker Desktop Stable 2.3.0.2 or a later release.

Make sure to select below during installation,

Run Docker Desktop.

That’s it.

You have now installed docker on WSL 2.

Verify it by running it in Ubuntu/Linux terminal.

Configure Docker container limits in WSL2

WSL 2 also allows us to configure memory and processors in below config, this can be controlled in case you want to limit resources used by your docker containers.

Add 

%UserProfile%\.wslconfig
 file for tweaking WSL2 settings

[wsl2] kernel=<path> # An absolute Windows path to a custom Linux kernel. memory=<size> # How much memory to assign to the WSL2 VM. processors=<number> # How many processors to assign to t
he WSL2 VM. swap=<size> # How much swap space to add to the WSL2 VM. 0 for no swap file. swapFile=<path> # An absolute Windows path to the swap vhd. 
localhostForwarding=<bool> # Boolean specifying if ports bound to wildcard or localhost in the WSL2 VM should be connectable from the host via localhost:port (default true).

Test Docker: Getting-Started

From the official documentation of Docker, lets test Getting-Started guide.

git clone https://github.com/docker/getting-started.git

Run below commands to build docker image,

Note that we are running this in WSL terminal of your linux distro.

Run 

docker run -d -p 80:80 --name docker-tutorial docker101tutorial

Open browser and type http://localhost/

You should see the getting started guide.

That’s it folks, we have successfully installed and tested Docker on WSL2.

I will test more features of Docker on WSL2 and update the article but the above steps should give you a head start to use Linux images in docker for windows.

Previously published at https://medium.com/codingfullstack/run-docker-linux-containers-natively-on-windows-subsystem-for-linux-4517584c86f5

Tags

The Noonification banner

Subscribe to get your daily round-up of top tech stories!