Can't connect to WSL2 based container via the web-browser

Let me preface this by saying that I know this has been asked before, but I have tried many solutions and nothing has worked.

System Specs

Physical System

Dell XPS 15

Windows 10 Pro build 19045.208

WSL

Results of wsl -v

WSL version: 1.2.5.0
Kernel version: 5.15.90.1
WSLg version: 1.0.51
MSRDC version: 1.2.3770
Direct3D version: 1.608.2-61064218
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.19045.3208

Ubuntu

Installed via the microsoft app store

Distributor ID: Ubuntu
Description:    Ubuntu 20.04.6 LTS
Release:        20.04
Codename:       focal

Docker

Docker version 24.0.4, build 3713ee1

Situation

I have several containers running in Docker:
enter image description here

The second container in this list provides a web based user-interface, accessed by going to a web browser and going to localhost:4443. If I just run this program in WSl2 and not in a container, it works fine. I need to run it in a container though. No matter what I have tried, I get that the site cant be reached.

These containers were built with a docker compose file.
In the compose file I set for the container I am interested in:

ports:
    - 5443:4443

What I have tried

  1. Forwarding the ports:
    Used this stack overflow answer: windows subsystem for linux - Can't connect to WSL2 localhost server from WSL2 docker container - Stack Overflow . Here is the result of netsh interface portproxy show all
Listen on ipv4:             Connect to ipv4:

Address         Port        Address         Port
--------------- ----------  --------------- ----------
*               3306        172.30.240.77   3306
*               1433        172.30.240.77   1433
*               1444        172.30.240.77   1444
*               8086        172.30.240.77   8086
*               8088        172.30.240.77   8088
*               8080        172.30.240.77   8080
*               8888        172.30.240.77   8888
*               3000        172.30.240.77   3000
*               22          172.30.240.77   22
*               4443        172.30.240.77   4443

  1. I had installed docker desktop at one point that caused a ton of problems after having used the linux version first in WSL2. I thought perhaps completely reinstalling everything would help so I completely removed Wsl2 and Ubuntu. Got the same issue with a clean install.

  2. Using the ip address instead of localhost. Tried connecting to 172.30.240.77:4443 and had no luck. Tried 127.0.0.1 and also had no luck.

Note

I must note that it randomly worked a SINGLE time. Not sure what I did, but it stopped working and has not worked since.

You installed Docker Desktop for Linux inside WSL2, instead of installing the Windows version on the host?

I installed the docker engine via command line in WSL2. Then I installed windows docker desktop. I did not initially want docker desktop but needed it later on. I removed everything though and started from scratch without docker desktop in windows.

If you run docker inside the WSL2 (regardless whether it is Docker Desktop for Linux or docker-ce), every port you publish, will be published inside the WSL2 distribution. If you bind a port in a WSL2 distribution, it will be mapped to the same port on Windows hosts localhost interface. The port will not be bound to any other host interface or ip.

If you need the containers to be reachable from the host-ip (e.g. from other hosts in your lan), you will need to manually configure portforwarding. Google should yield plenty of blog posts that explain how to forward traffic from the host ip to WSL2 distributions.

I run docker-ce in a wsl2 distro, and I can reach published container ports in Windows using localhost:{host port}.
If this doesn’t work, you might want to check if a firewall or a vpn connection is responsible that the communication the host and the wsl distribution is not working.

Thank you for your response. The first thing I tried above was to manually forward the ports, I have included the configuration above. Isn’t that what you are talking about?

The take away should have been, that if you run a self installed docker engine inside a WSL distribution, then publishing a port like this:

Should be reachable on the Windows host from localhost:5443, without having to configure anything.

Your problem is caused by how WSL2 works. The docker engine installed inside the WSL2 distribution is neither part of the problem, nor part of the solution.

Thanks but your last sentence has left me confused. You state that what I should have taken away was that what I did should be working but then say its caused by how WSL2 works. Looks like its likely a firewall issue of some sort, correct?

Also, like I said, I can access the WSL2 instance application when running the application without docker. It is only when I run the container that I have this issue.

Tried this: networking - WSL2 connect to host without disabling the windows firewall - Super User

Disabled the firewall and still have the same issue.

Try it yourself:
Start a nginx container in your wsl distro with port 80 published:

sudo docker run --rm -ti -p 80:80 nginx

Then you should be able to access it from the Windows host in a browser with http://localhost, or from the terminal with curl http://localhost

if the host port is already bound by another port use -p {whatever port}:80 when starting the container and http://localhost:{whatever port} when opening the url in the browser and querying it with curl.

This must work out of the box.

1 Like

I have the same issue. Were you able to solve yours??

I had similar problem, but i overcame it by removing all port proxies one after the other. I used this commands:
To show all ports proxy: netsh interface portproxy show all
To delete each of them: netsh interface portproxy delete v4tov4 listenport=3000 listenaddress=172.17.0.3

1 Like