The Tech Platform

Dec 6, 20212 min

Docker Engine on WSL + Visual Studio 2019 without Docker Desktop

Prerequisite

  • Install WSL version 2 (see Microsoft documentation)

  • Install Linux (personally I choose Ubuntu)

  • Install Visual Studio 2019

  • Clone this repository

Configure WSL

You maybe need to configure WSL to limit processor and memory usage.

This can be done with the file .wslconfig in your Windows profile folder (%USERPROFILE%).

[wsl2]
 
memory=4GB # Limits VM memory in WSL 2 up to 4GB
 
processors=2 # Makes the WSL 2 VM use two virtual processors

See Microsoft documentation.

Install Docker tools

Install Docker

From Docker documentation :

sudo apt-get update
 
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release
 
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
 
echo \
 
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
 
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
 
sudo apt-get install docker-ce docker-ce-cli containerd.io
 
sudo docker run hello-world

You will have an error here :


 
Docker service didn’t start automatically in WSL, even if you reboot Ubuntu, since systemd is not enabled.


 

You can start it manually :

sudo service docker start

Allow Docker without sudo (see Docker post installation documentation)

sudo groupadd docker
 
sudo usermod -aG docker $USER
 
newgrp docker
 
docker run hello-world

Install Docker Compose

sudo apt update
 
sudo apt install docker-compose

Start multi-container with Docker Compose

This docker-compose.yml is configured to start a Kafka environment and the listener application. If you open it, you will see the listener configuration :

services:
 
listener:
 
container_name: listener
 
image: ${DOCKER_REGISTRY-}listener
 
depends_on:
 
- rest-proxy
 
build:
 
context: .
 
dockerfile: KafkaListener/Dockerfile
 
ports:
 
- "80"
 
environment:
 
ASPNETCORE_ENVIRONMENT: Development
 
ASPNETCORE_URLS: http://+:80
 
ConsumerConfig__BootstrapServers: kafka:9092
 
ConsumerConfig__GroupId: wsl-consumer
 
ListenerConfig__Host: http://172.17.0.1:5002
 
ListenerConfig__Route: /hello
 
ListenerConfig__Topic: hello-topic

The address 172.17.0.1 is the address of Docker.

So let’s start the environment:

docker-compose up -d --build && docker attach listener

Now let’s start the application to rename :

dotnet run --project ./WslApplication/WslApplication.csproj --urls "http://172.17.0.1:5002"

Ok, now we send a message to Kafka :

To communicate with the container from Windows, you must use this address [::1].


 
You should see this message in both applications :


 

Visual Studio 2019

Open the solution DockerWSL.sln, and choose the WSL profile :


 
If you have this error, you have to select Ubuntu as the default WSL :

wsl -s Ubuntu-20.04

You may have also those errors :

Just click install and it will work.

Now you can work and debug directly with Visual Studio.

So, the solution work and it’s easy to implement.

The downside I see is that we lose the concept of “Works on my machine” since the application no longer works on Docker but on WSL.

Source: Medium - Frcs6

The Tech Platform

www.thetechplatform.com

    0