Build Containerised .NET Core Application using Docker on Windows Subsystem for Linux (WSL) 2

Docker Container WSL2

When first Windows Subsystem for Linux (WSL) launched, I tried to use it as a Linux development platform and ran my first Cross Platform .NET Core Web Application and I found it amazing.  

With the popularity of Microservices, containerised applications with Docker is one of the hottest topic in the market. While learning this piece of tech I thought of using my WSL to use it as my Docker development platform natively on experimental basis.

While doing many failed attempts to install and trying to configure Docker in WSL1, Microsoft announced the release of WSL2 on their 2019 Build conference.

Also, it was announced that a full-blown Linux Kernel is shipping with Windows 10. Naturally my expectation for WSL2 raised higher and higher with it, and my wish of using WSL for Dockerized application development has become a reality.

In this article I am going to talk about how you can get started with WSL2, installing Docker in WSL2 natively and finally running a .NET Core web app from a Docker Container.

First Thing First. Make your PC ready for WSL2

Get the Right Windows 10 Build: 

WSL2 is only available for Windows 10 version 1903, Release 18917 or higher.  If you are Windows Insider then you have to download this release as a very first pre-requisite.

Enable Hyper-V:

Follow this link if you haven’t enabled Hyper-V in your windows 10.

Install WSL2:

Now that the basic time consuming part is done, time to setup WSL2. Fire up your PowerShell terminal in Administrative mode and run the below commands.  This will enable virtualisation feature which will be used by WSL2

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

Next you have to find out your Installed Linux Distribution, so that you can set that distribution to work as WSL version 2.

wsl -l -v

If you do not have WSL installed already then check this out and install your WSL.

Now with patience run the below command. This will convert your current Linux Distribution to WSL version 2. This step took 25 minutes for me to complete 🙁

wsl --set-version Ubuntu 2

Final step, If you want to use your WSL2 as default version then execute below command

wsl --set-default-version 2

This completes your WSL2 installation in your Windows 10 PC. For detailed installation guide check this out. Next is to try installing Docker on WSL for which I banged my head like anything in WSL1 world.

Install and run Docker on WSL2

Open your WSL2 terminal and check whether Docker is present in your system or not by executing –

docker --version

If you receive -bash: /usr/bin/docker: No such file or directory then you know that Docker is no present in your system and you are good to go to install Docker in WSL2.

Step 1: Update your Linux software repository

sudo apt-get update

Step 2: Download Docker Dependencies

sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

 Step 3: Add Docker PGP key

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –

Step 4: Install Docker Repository

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

Step 5: Update software repository

sudo apt-get update

Step 6: Install latest Docker version

sudo apt-get install docker-ce

This step completes Docker Installation on your WSL2, now you have to do a quick rain check.

Step 7: Check Docker Version
To check whether docker is installed properly or not check Docker version using this command docker --version. If you don’t get error like – bash: /usr/bin/docker: No such file or directory and see the Docker version then you know that Docker is installed successfully.

Now that Docker is installed, you need to start the docker service. To check whether Docker Service is running or not execute below commands

Step 8: Check Docker service status
service docker status
or
sudo service docker status

Step 9: Running Docker service
sudo service docker start

Look for OK in your terminal. To re-verify service status use this command

sudo service docker status

Step 10: Check Docker container and image status

If you want to know more information about running Docker Images use
sudo docker info 

or sudo docker ps

This will show you status of all your image details. At this point in time it won’t show anything as we haven’t created any Docker container yet.

In next step we are going to create a Docker container for .NET core application and run it, then we will re-visit these stats.

Note: you can run docker ps and docker info without sudo. If you get any socket error while running docker ps and docker info without sudo then run this command sudo usermod -a -G docker $USER and completely logout from the terminal. Now you should be able to run these commands without sudo.

Dockarize your .NET Core Web app

So far we have covered how to Confiure WSL and Install Docker on it and run it, now it’s time to Dockerize an existing .NET Core MVC application and running it.

For this example I have created a simple .NET Core app with mvc template outside of WSL using

dotnet new mvc

Created a new directory named first-docker on WSL and copied the above .NET project contents. Opened a Visual Studio Code editor from the current directory. Added the following Dockerfile

I’m calling this application as first-docker and hence the output dll is first-docker.dll. See Line 16.

Added a .dockerignore file to exclude bin and obj folder. Added the following lines into it.

/bin
/obj


Now that the Dockerfile is added, it’s time to build the Docker container with all the .NET runtime along with building the project inside.
Use the following command in the WSL2 bash within the current working directory

docker build -t first-docker .

It will start installing required package to build .NET Core Container as below –

On successful build of the Image you will see something like this

To run your first Dockerized .NET Core app execute up the following command

docker run -d -p 8080:80 --name dotnetcoredocker first-docker

This will run the .NET Core MVC app and will start listening to 8080 port. To access it from your Windows 10 browser you have to open the url with IP address. You Can’t use localhost at this moment, at the time of writing this post localhost is not supported in WSL2.
To get your WSL2’s IP use the following command

ip addr | grep eth0

Once you get your IP address type http:///
in a browser running on your Windows 10, and Viola…!!! you got your first Dockerized .NET Core MVC Web App running on WSL2.

Final check and wrap up

Now that your first Dockerized .NET Core application is running perfectly well on WSL2 let’s check few final things –

Let’s go up and check Docker Setup Step 10. While running docker ps we have seen that there was no running container and also in the output of docker info command number of containers and images was 0.

While the Dockerized Web Application is running in browser, let’s run those commands again and see the difference –

docker info

docker ps

Notice that Container and Running instance count increased to 1 from 0. An Image with name first-docker is running in a container named dotnetdocker and it’s listening to 8080 port as configured in the Dockerfile.

This completes the full circle, starting from Installing WSL2 in your Windows 10 PC, Installing Docker and finally running a Dockerized .NET Core Web application from WSL2. Enjoy your first Dockerized app on WSL2. Cheers…!!!