Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access a service running on WSL2 from inside a Docker container?

I am using Windows 10 1909 and have installed WSL2, using Ubuntu 20.04, the 19.03.13-beta2 docker version, having installed Docker for Windows Edge version using the WSL2 option. The integration is working pretty great, but I have one issue which I cannot solve.

On the WSL2 instance, there are services running, exposing some ports (3000, 3001, 3002,...). From one of the docker containers, I need to access the services for a specific development scenario (API Gateway), and this I cannot get to work.

I have tried using the WSL2 IP address directly, but then the connect just times out. I have also tried using host.docker.internal, which resolves to something else than the WSL2 IP address, but it still doesn't work.

Is there a special trick I need to pull, or is this kind of routing currently not supported, but will be, or is this for some other reason not possible?

This illustrates what I am trying to achieve:

Accessing WSL2 services from docker container

The other routings work - i.e. I can access all the service ports coming from the node.js processes inside WSL2 from the Windows browser, and also I can access the exposed service ports from the containers both from inside WSL2 and from Windows. It's just this missing link I cannot make work.

like image 474
donmartin Avatar asked Sep 15 '20 08:09

donmartin


People also ask

How do I access a service inside a container?

To access the service from inside the container you need the port that particular host is listening to as no matter where the service is running we need to access it through the host node. If you have a service running on some other port you can access it via 172.17. 42.1:5432.

How do I access Docker from WSL2?

Right-click the icon to display the Docker commands menu and select "Settings". Ensure that "Use the WSL 2 based engine" is checked in Settings > General. Select from your installed WSL 2 distributions which you want to enable Docker integration on by going to: Settings > Resources > WSL Integration.

How can I see what process is running inside a container?

Like it was mentioned, if you are already inside of a container, then just use ps -eaf command to see the running processes.

Can I run a Windows service in a Docker container?

However, Docker for Windows does allow you to install and run services in docker containers, as long as there is a primary process which keeps the container alive. In the official Microsoft images microsoft/iis and microsoft/aspnet , a program called ServiceMonitor.exe is used as the primary process.


2 Answers

For what it's worth: This scenario is working if you use the WSL2 subsystem IP address.

It does not work if you use host.docker.internal - this DNS alias is defined in the containers, but it maps to the IP address of the Windows host, not of the WSL2 host, and that routing back inside the WSL2 host does not work.

The reason why this (probably temporarily) did not work is somewhat unclear - I will revisit this answer if the problem should reappear and I manage to track down what the actual problem may have been.

like image 52
donmartin Avatar answered Sep 30 '22 01:09

donmartin


So what you need to do in the windows machine port forward the port you are running on the WSL machine, this script port forwards the port 4000

netsh interface portproxy delete v4tov4 listenport="4000" # Delete any existing port 4000 forwarding
$wslIp=(wsl -d Ubuntu -e sh -c "ip addr show eth0 | grep 'inet\b' | awk '{print `$2}' | cut -d/ -f1") # Get the private IP of the WSL2 instance
netsh interface portproxy add v4tov4 listenport="4000" connectaddress="$wslIp" connectport="4000"

And on the container docker run command you have to add

--add-host=host.docker.internal:host-gateway

or if you are using docker-compose:

    extra_hosts:
      - "host.docker.internal:host-gateway"

Then inside the container you should be able to curl to

curl host.docker.internal:4000

and get a response!

like image 44
ferluis Avatar answered Sep 30 '22 00:09

ferluis