DEV Community

Cover image for 🔝Running docker containers in Windows Subsystem for Linux (WSL2)🔥🔥🔥
David
David

Posted on

🔝Running docker containers in Windows Subsystem for Linux (WSL2)🔥🔥🔥

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

Long story short, try the below code snippet in Windows 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
Enter fullscreen mode Exit fullscreen mode

Download desired Linux system

Open the Microsoft Store 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
image
image

Set your distribution version to WSL 1 or WSL 2

wsl --list --verbose
wsl --set-version <distribution name> 2
Enter fullscreen mode Exit fullscreen mode

Make sure to replace with the actual name of your distribution, e.g. "Ubuntu-20.04"

Install docker on WSL2

The instructions can be found here, but here are the quick scripts for installing Docker on Ubuntu:

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
Enter fullscreen mode Exit fullscreen mode

Run docker daemon in WSL2

sudo service docker start # (a) Run docker service, recommended
sudo dockerd              # or (b) this one also works 
Enter fullscreen mode Exit fullscreen mode

image

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, but you can use whatever terminal tools, e.g. Cmder or Fluent Terminal etc.)

docker run hello-world
Enter fullscreen mode Exit fullscreen mode

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

image
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:
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
Enter fullscreen mode Exit fullscreen mode
  • Build and run the docker instance
docker build -t "webserver" .
docker images
docker run -d -p 80:80 webserver /usr/sbin/apache2ctl -D FOREGROUND
Enter fullscreen mode Exit fullscreen mode

If you see some output like this, it is a success

image

  • Try your Apache web server on your windows host:

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

image

Or run in terminal:

curl localhost
Enter fullscreen mode Exit fullscreen mode

image

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

Top comments (0)