Docker for Window with Subsystem for Linux (WSL)
Tags: docker, devops, windows
Published: 2020-06-17 20:13:50

Modern project development often requires developers to understand not one or two core technologies, but a whole range of them. It can involve different programming languages, structures, and databases or cloud operating systems. However, one of the main challenges is scaling and configuring the interactions and dependencies of these components between themselves.

To solve such tasks, companies and developers often resort to container technology, which integrates and packages all kinds of technologies, programs, and code into one ecosystem in the form of containers. One of the most popular solutions in this area is [Docker](https://www.docker.com/) Containers, which has such advantages:

* isolate environment,
* scaling options,
* dependency management,
* host autonomy,
* simple software layout.

At the same time, we should note that the basic operating system for Docker is Linux. However, for developers who prefer Windows, some solutions use virtual machine technologies - [Docker for Windows](https://docs.docker.com/docker-for-windows/) and [Docker ToolBox](https://docs.docker.com/toolbox/). But their use is not always practical, as virtual machines often require additional system resources and more advanced configuration options for interaction with the main operating system.

There is another approach for such cases that uses [Windows Subsystem for Linux](https://docs.microsoft.com/en-gb/windows/wsl/about) (WSL) and allows running Linux programs, including Docker, on the Windows platform.

#WSL aspect and configurations#

Windows Subsystem for Linux provides functionality for Linux files, scripts, and programs in Windows 10 and Windows Server 2019. WSL does not use virtual machines but runs a compatibility layer at the system level. It provides a high integration level with the main system and allows to use most programs from repositories for Linux distributions without significant system resources.

Let’s highlight the main WSL features and aspects of its application:

* Uses only the latest Windows 10 revisions and provides the ability to run multiple Linux distributions and associated programs.
* WSL is not a complete Linux kernel, but just a compatibility functionality for running Linux programs. Therefore, for low-level support, it requires other tools such as virtual machines.
* WSL can be used effectively for simple and not heavily loaded high-level programs or scripts, for example, for the bash-environment.

In this case, for complete integration of Docker with the Windows platform, we will use the latest version of the program - WSL2. It will allow avoiding the stage of creating a private virtual machine for Docker and therefore simplify its use due to system resources and data synchronization between different systems.

Complete installation procedure of the WSL2 (after this simply WSL) is fully described in the official [documentation](https://docs.microsoft.com/en-us/windows/wsl/wsl2-install) and usually does not cause any problems. Therefore, below we will only focus on the main aspects of installation and configuration.

The main steps in WSL configuration for Windows include the following:

* Checking the operating system build version.
* Creating or logging on to [Microsoft Account](https://account.microsoft.com/account) and joining the [Windows Insider Program](https://insider.windows.com/en-us/).
* Enabling software options and operating system settings: 
  - WSL and virtual machine platform support service. 
  - [Hyper-V](https://docs.microsoft.com/en-gb/virtualization/hyper-v-on-windows/quick-start/enable-hyper-v) virtualization services (required for Docker for Windows).
* Selection of the Linux distribution and its further configuration.
* Upgrades of Linux components to required or current versions.

Next, we will show and describe these steps briefly.

There are two ways to check the version of the operating system Windows 10. The first is to use the Windows interface and execute the `winver` command on the run command line. The second uses the command-line interface (or PowerShell terminal) and the execution of the `systeminfo` command. As a result, you will get the operating system version and it should be **Windows Pro 10**, higher than the 18917 build or **Windows Pro 10 Insider** version as in our case.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/svjpakwqd1bdqh4noqar.png)

*Note that Windows 10 must be PRO version as for other ones, the Hyper-V virtualization service will not be configured, and the Docker for Windows will not work*.

Once the operating system version matches the requirements, you will need to join the [Windows Insider Program](https://insider.windows.com/en-us/). You can do this by opening the next options:

1.Select *Settings*>*Update & Security*.
2.Select the *Windows Insider Program* tab.
3.Login with a Microsoft Account.
4.Press the *Run* button and select the *Fast* option.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/p4jrtqqk6u1sic3zofvy.png)

*Note that if you do not get the above option, you need to become an administrator or check the operating system version again*. 

Next, we will enable necessary services and options in the Turn features on or off section as shown below :

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pt1hhy32foq5e9in6j88.png)

The same can be done with the following commands in a PowerShell terminal.

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

```
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform
```

```
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V –All
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/121pp9xs6ohikqy6ysft.png)

Following that, we will use the Microsoft Store and choose one of the free versions of Linux. In our case, it will be a popular [Ubuntu 18.04 LTS](https://www.microsoft.com/en-us/p/ubuntu-1804-lts/9n9tngvndl3q?ocid=AID2000142_aff_7593_1243925&tduid=(ir__adbla6mhygkftlhekk0sohz3xm2xlsto1cvcfgzt00)(7593)(1243925)(kXQk6.ivFEQ-PQalRznbQq10vRr4ZqYevA)()&rtc=1&ranMID=24542&ranEAID=kXQk6*ivFEQ&ranSiteID=kXQk6.ivFEQ-PQalRznbQq10vRr4ZqYevA&epi=kXQk6.ivFEQ-PQalRznbQq10vRr4ZqYevA&irgwc=1&irclickid=_adbla6mhygkftlhekk0sohz3xm2xlsto1cvcfgzt00&activetab=pivot:overviewtab) image.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lkpv3jgriiqoi9umb703.png)

Before running the downloaded image, let's make sure that we are using the latest, which is the second version of the WSL. To check this, let's enter the commands in the PowerShell terminal:

```
wsl -l –v
wsl --set-version ubuntu 18.04 2
```

The second command is only required if you have installed the first version of WSL. Differences between versions and how to manage WSL images are well described in [documentation](https://docs.microsoft.com/en-gb/windows/wsl/wsl2-ux-changes) and [tutorials](https://docs.microsoft.com/en-gb/windows/wsl/wsl-config).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/w1ilwj0vx1ma8ponh5e8.png)

Following that, we will launch WSL from the Start menu, and after initial configuration, we'll get the Ubuntu terminal as shown below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g3r3x49ip3x9d919kq0a.png)

Finally we are ready to install Docker.

#Docker Desktop with WSL and Docker-CE#

Currently, there are two options for installing Docker on Windows 10:

* Using [Docker Desktop for Windows](https://docs.docker.com/docker-for-windows/) with WSL2 backend.
* Using WSL2 tools to install the Linux version of [Docker-CE](https://hub.docker.com/editions/community/docker-ce-desktop-windows).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/k1zaaxqn1sgrs3js2016.png)

The first option is appropriate for developers who build their projects on Windows platform and need native Linux kernel support for Docker. That makes it possible to run containers without emulation and use the Linux workspaces. Also, we should mention that Docker with WSL2 makes the system resources (specifically, memory) less demanding since it uses the dynamic memory allocation function.

The second option provides functionality of the Linux environment, which gives an advantage for projects using its tools and repositories. Note that Docker-CE does not require a basic desktop version and the Hyper-V service. Therefore, this option allows you to run other virtual machines together with WSL for other different tasks.

In general, the usage of Docker-CE and WSL does not differ from the typical Docker usage on Linux systems. Therefore, we will only focus on configuration and customization of Docker Desktop being a popular tool for managing containers on the Windows platform.

#Docker Desktop with WSL2 backend#

Let's install the latest version of Docker Desktop from the official website. To download the install package and get started, you will need to log in or create a new account on [Docker Hub](https://hub.docker.com/). 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4snslma970air5xx6do9.png)

Follow the instructions of the installer during the installation process. The process itself takes some time, following that, the computer will require a restart to enable the Hyper-V service.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5n528o3j8wx0eb417qae.png)

If the installer reports a firewall or antivirus blocking during the installation, you will need to add port 445 to the firewall exceptions or rules.

After installation and rebooting, the Docker Desktop will automatically launch. During the first start, you will need to enter Docker Hub Credentials to initialize all involved services.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/v3qhk84mliy3iky7yw52.png)

Once the installation is complete, the Docker Whale icon will appear in the taskbar, and by clicking on it, we can configure it further. 

Next, we will initialize the WSL2 backend functionality in the installed application. To do this, select ***Settings*** from the menu and activate WSL2 backend engine on the ***General*** tab.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/38bs14ll3qafv39y0pco.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/syuejcc1h6rfwev6t6lf.png)

After applying these actions, you will need to reboot the Docker application and then confirm the activation of WSL integration for Docker Desktop.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0f4tfccc1bnqv128tdt4.png)

Next, you should activate the previously installed WSL Ubuntu 18.04 LTS image in Docker settings under the ***Resources*** tab.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/03uqt0qshuzzs4i2jeek.png)

After implementing the changes, let us check to see if everything has been installed correctly. On the PowerShell terminal using the standard `docker version` command:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hlr2npbgrzujwbx6rf7a.png)

As a result, we can see that Docker Desktop has been installed correctly and is ready for use. But before connecting, let's highlight some features of using it on a Windows system:'

* If you want to connect Windows or Linux containers, you should change the corresponding option in Docker settings as shown below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rpe8haepzlwzmb1uwexr.png)

* Project files should be stored in proper folder and environment to avoid case-sensitive errors in the Windows project. One of the solutions that will help to use Windows project files in a Linux environment is  the `mklink` command which creates a symbolic link to the project folder.
`mklink /D C:\project_directory \\wsl$\ubuntu-18.03\home\user\project_directory`

* Local domains should be registered in their respective host's file. Windows 10 host files are located in: **C:\Windows\System32\drivers\etc\hosts**

Other features and options for using Docker on Windows are generally the same as in the Linux version. 

Next, let's run the simplest examples to demonstrate the features of Docker Desktop with WSL.

#Running Docker on Windows#

To demonstrate Docker functionality, we will use Docker Hub, which contains most ready-to-use containers.

As an example of Docker's workability, we will take the simplest container that displays the phrase ["Hello-World"](https://hub.docker.com/_/hello-world). To do this, we need to run two commands on the command line or the PowerShell terminal.

`docker pull hello-world`

`docker run hello-world`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3e8mwabpzj2xgp3oelof.png)

As we can see, the messages are displayed, and Docker Desktop works correctly. Next, let’s try to run the Windows Server container from its official image. To do this, let's run the following commands:

```
docker run mcr.microsoft.com/windows/servercore:ltsc2019
```

```
docker run --rm –it mcr.microsoft.com/windows/servercore:ltsc2019 cmd
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ex420dx8il65nl6r5uha.png)

The first command downloads and runs the associated container, while the second command runs it with parameters that will output the container in the command line environment as shown below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ia0yzesfzjf7o90971bt.png)

As a result, we have the Windows operating system running in the container. For test, we've also executed the standard command `dir` and `tasklist` and everything works correctly.

Now, we will create our container on the Docker Hub with the output of the test phrase at its startup. To do this, first of all, let's check the downloaded containers.

`docker images`

From the container ID, we will create a new one in our repository on Docker Hub with the following command:

```
FROM 8351e66084ac `n CMD echo Test Work! Hello World!" | docker build -t philkaua/windows-test-image –
```

After executing this command, a new container will be created. We will check its performance by running it from the PowerShell terminal using the command:

`docker run <user>/windows-test-image`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/p7ell38h3d0spkkqys3f.png)

As we can see, the container is working.

#Summary#

In this article, we showed how to use containers using Docker on a Windows platform. We walked through the installation and configuration features of the Windows Subsystem for Linux as well as Docker for Windows systems. Perhaps this information will help and simplify the use of container technology with Docker and Windows.