🔝Running docker containers in Windows Subsystem for Linux (WSL2)🔥🔥🔥
Tags: docker, wsl2, wsl, container
Published: 2021-05-04 03:18:42

The Windows Subsystem for Linux **(WSL)** 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.

**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.

# Install WSL2 on Windows

For the full instructions to install WSL2 on windows, refer to [this link](https://docs.microsoft.com/en-us/windows/wsl/install-win10)

Long story short, try the below code snippet in Windows Powershell:

```Powershell
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
wsl --set-default-version 2
```
## Download desired Linux system

Open the [Microsoft Store](https://aka.ms/wslstore) and select your favorite Linux distribution. For instance:

- Ubuntu 20.04 LTS
- openSUSE Leap 15.1
- SUSE Linux Enterprise Server 15 SP1
- Kali Linux
- Debian GNU/Linux
- Fedora Remix for WSL
- Pengwin Enterprise
- Alpine WSL

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/af9264om1j1f9145gajz.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rus44gp3l5c8bxz7zo06.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ke9bpce2ts4fitv1p8z2.png)

## Set your distribution version to WSL 1 or WSL 2 

```Powershell
wsl --list --verbose
wsl --set-version <distribution name> 2
```
Make sure to replace <distribution name> with the actual name of your distribution, e.g. **"Ubuntu-20.04"**
  
## Install docker on WSL2

The instructions can be found [here](https://bootstrap-it.com/docker4aws/#install), but here are the quick scripts for installing Docker on Ubuntu:

```bash
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

sudo apt-get update
sudo apt-get -y install docker-ce docker-ce.cli containerd.io
```

## Run docker daemon in WSL2

```bash
sudo service docker start # (a) Run docker service, recommended
sudo dockerd              # or (b) this one also works 
```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rkwndd59wqq13vwnsu5d.png)
 
## Build and run a container app

If you are using approach (b) to start docker service, remember to **open a new Ubuntu terminal** (I am using [Windows Terminal](https://github.com/microsoft/terminal), but you can use whatever terminal tools, e.g. [Cmder](https://cmder.net/) or [Fluent Terminal](https://github.com/felixse/FluentTerminal) etc.)

```bash
docker run hello-world
```
This will run the 'hello-world' app in your container!

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t5rl5bpopxtmadhiaa7g.png)
Congrats! You have run the simplest Linux container App in WSL2!


## Run your customized container app using Dockerfile

- Create a new file called 'dockerfile', where Apache2 web server is installed, and a simple html file is hosted:

```dockerfile
FROM ubuntu:latest
RUN apt-get update
RUN DEBIAN_FRONTEND="noninteractive" apt-get install -y apache2
RUN echo "Welcome to my web site" > /var/www/html/index.html
EXPOSE 80
```
- Build and run the docker instance

```shell
docker build -t "webserver" .
docker images
docker run -d -p 80:80 webserver /usr/sbin/apache2ctl -D FOREGROUND
```
If you see some output like this, it is a success

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51tc5k9ob5tbohbmnzmv.png)

- Try your Apache web server on your windows host:

Open a browser and navigate to `http://localhost:80` 

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8c48qhnsyukiaaolpb8c.png)

Or run in terminal:

```
curl localhost
```

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qsdwiocsok6n33m21swk.png)
 

Congrats! You have run your customized Linux container App in WSL2!