WSL + Docker without Desktop — Communicate with Windows Host

Frcs6
4 min readSep 9, 2021

--

Preface

This post present a solution to stop using Docker Desktop and use only Docker Engine on WSL.

I decide to use :

  • WSL2
  • Ubuntu 20.04
  • Docker Engine for Linux
  • Docker Compose for Linux

I want to do something like this :

Prerequisite

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

From Docker documentation :

WSL TERMINAL :
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.

WSL TERMINAL :
service --status-all

You can start it manually :

WSL TERMINAL :
sudo service docker start

Allow Docker without sudo (see Docker post installation documentation)

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

Install Docker Compose

WSL TERMINAL :
sudo apt update
sudo apt install docker-compose

Start multi-container with Docker Compose

Let’s try to start a Kafka environment with Docker Compose, my docker-compose.yml file can be found in the src folder.

WSL TERMINAL :
docker-compose up -d
WSL TERMINAL :
docker-compose ps

Ok, we have a Kafka environment in WSL, now let’s try to send a message.

WSL TERMINAL :
curl http://localhost:8082/topics -w "\n"
WINDOWS POWERSHELL :
Invoke-WebRequest -Uri 'http://[::1]:8082/topics/hello-topic' -Method POST -ContentType 'application/vnd.kafka.json.v2+json' -Body '{"records":[{"value":{"Message": "Hello Kafka!"}}]}' -UseBasicParsing
WSL TERMINAL :
curl http://localhost:8082/topics -w "\n"

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

Communicate with Windows from the containers

Ok, now let’s try to communicate with Windows host. You can read the Microsoft documentation.

First, we have to find his IP address :

WSL TERMINAL :
cat /etc/resolv.conf

Secondly, we have to configure the firewall to authorize communication :

WINDOWS POWERSHELL :
# Run as Administrator
New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action Allow

Finally, we start Kafka environnement with a listener application.

Note : you have to fix the IP address in the file docker-compose.listener.yml :

ListenerConfig__Host: http://172.17.80.1:5002

Now start Kafka and the listener :

WSL TERMINAL :
docker-compose -f docker-compose.yml -f docker-compose.listener.yml up -d --build && docker attach listener

Then, let’s start an application on the host to handle HTTP message :

WINDOWS POWERSHELL :
# Use the WSL IP address
dotnet run --urls "http://172.17.80.1:5002"

Send a message to Kafka :

WINDOWS POWERSHELL :
Invoke-WebRequest -Uri 'http://[::1]:8082/topics/hello-topic' -Method POST -ContentType 'application/vnd.kafka.json.v2+json' -Body '{"records":[{"value":{"Message": "Hello Kafka!"}}]}' -UseBasicParsing

You should see this message in both applications :

Conclusion

This solution work, but I found the following irritating:

  • Dealing with the firewall is not simple
  • The Host IP address changes

While I was testing the solution, I had the conviction that the Windows Host is useless.

In a next tutorial, I will present a solution that uses only WSL. Furthermore, Visual Studio 2019 work very well with WSL.

--

--