Skip to content

Can't connect to docker container running inside WSL2 #4983

Closed

Description

  • Your Windows build number: Microsoft Windows [Version 10.0.19041.153]

I'm running a couchbase server inside WSL2 with multiple port-forwarding :

(from Windows) ❯ wsl -l -v
  NAME      STATE           VERSION
* Ubuntu    Running         2
(inside WSL) ❯ docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS
                                                                                           NAMES
aaad5f11ad16        couchbase:community   "/entrypoint.sh couc…"   17 minutes ago      Up 4 minutes        0.0.0.0:8091-8096->8091-8096/tcp, 0.0.0.0:11207->11207/tcp, 0.0.0.0:11210-11211->11210-11211/tcp, 18091-18096/tcp   couchbase

Everything seems to work, I can connect to localhost:8091 from the Windows Host, I can setup the Couchbase and if I try to connect to the different ports exposed by the container, things look nice and clear:

(from Windows) ❯ Invoke-RestMethod -Uri http://localhost:8092

couchdb version couchbase
------- ------- ---------
Welcome         6.5.0-4966-community

(from Windows) ❯ Invoke-RestMethod -Uri http://localhost:8093
Invoke-RestMethod: 404 page not found

(from Windows) ❯ Invoke-RestMethod -Uri http://localhost:8094
Invoke-RestMethod: {"error":"Page not found","status":"fail"}

The problem is that if I try to connect to the same ports using a Java server that I'm building, I get a Timeout, 100% of times.

It worked until yesterday if I put the IP address of the WSL VM instead of localhost in the configuration file of my server (not the best solution, but at least it worked), then I updated the kernel of the WSL - because I was forced to - and it stopped working.

Even after restarting the lxssservice the problem persists.

The only thing that works is if I start the docker container with --net=host but then I can't start multiple instances of the same container because of the ports binding..

I don't know if it's related but when I run a netstat on the WSL machine it seems like only tcp6 bindings have been created but it seems to work if I force a tcp4 connection:

(from WSL) ❯ sudo netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp6       0      0 :::11207                :::*                    LISTEN      1510/docker-proxy
tcp6       0      0 :::11210                :::*                    LISTEN      1496/docker-proxy
tcp6       0      0 :::11211                :::*                    LISTEN      1481/docker-proxy
tcp6       0      0 :::8091                 :::*                    LISTEN      1593/docker-proxy
tcp6       0      0 :::8092                 :::*                    LISTEN      1579/docker-proxy
tcp6       0      0 :::8093                 :::*                    LISTEN      1566/docker-proxy
tcp6       0      0 :::8094                 :::*                    LISTEN      1552/docker-proxy
tcp6       0      0 :::8095                 :::*                    LISTEN      1538/docker-proxy
tcp6       0      0 :::8096                 :::*                    LISTEN      1524/docker-proxy
(from WSL) ❯ telnet -4 localhost 8091
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
^]
telnet>

If someone has a better solution than the --net=host I'm all ears..

Thank you everyone in advance, small bugs aside, WSL (and WSL 2 in particular) is what made me get back to Windows as my daily driver.

Activity

  1. jishi commented on Mar 23, 2020

    @jishi

    I had the same problem, and it turned out that WSL2 only binds to ipv6 localhost ([::1]) and not ipv4 localhost (127.0.0.1). It took me a while to figure out, because some applications default to ipv6 resolve first, and other apps (like node.js) defaults to ipv4 lookup. Hence, the host localhost works in your browser, but not in, lets say, node.js. I'm thinking Java does the same, since you are experiencing this problem.

    I resorted to using the ipv6 localhost url, http://[::1]:8091 and it just worked.

    When you test in WSL2 and connect to localhost with -4 flag, it works because from the WSL side of things it is acessible both via ipv4 and ipv6. From my experience, even if it lists only ipv6 listings in netstat, it does still listen on ipv4, but maybe this is actually the problem, the Windows bridge only forward traffic for the netstat listing and errorneously only binds ipv6 from the host into the WSL subsystem.

    I hope this helps.

  2. koying commented on Jul 21, 2020

    @koying

    As doing a search on a similar issue brought me here

    The workaround [::1] works for ipv6-enabled app, but if you need ipv4, an option is to use the WSL2 subsystem ip adress instead, i.e. the adress returned by doing a ifconfig inside wsl.

    
    $ ifconfig
    eth0: flags=4163  mtu 1500
    [...]
            inet 172.19.171.88 netmask 255.255.240.0  broadcast 172.19.175.255
    [...]
    

    Still quite inconvenient as I think this address changes at every restart of WSL.

  3. icorbrey commented on Jul 22, 2020

    @icorbrey

    Chris Browet (@koying) The subsystem IP did not work for me, but using the IPv6 loopback address like Jimmy Shimizu (@jishi) recommended works perfectly!

  4. koying commented on Jul 22, 2020

    @koying

    Oh sure. My workaround is only for non-ipv6 enabled apps.

  5. icorbrey commented on Jul 22, 2020

    @icorbrey

    Ah, that makes sense

  6. otavionetoca commented on Aug 12, 2020

    @otavionetoca

    It worked for me Jimmy Shimizu (@jishi) thanks!

  7. rvangala936 commented on Oct 8, 2020

    @rvangala936

    I have tried ip address of both "ip from ifconfig inside wsl" and "http://[::1]:port" and neither worked for "Ubuntu-20.04" on Windows 10 via Wsl2

  8. BrianLeishman commented on Oct 8, 2020

    @BrianLeishman

    If you're using MySQL Workbench, make sure to leave out the brackets and use ::1 as the host

    image

  9. canove commented on Oct 14, 2020

    @canove

    I resorted to using the ipv6 localhost url, http://[::1]:8091 and it just worked.

    In my case, MySQL Workbench is the only SQL Viwer app that works with localhost. Beekeeper Studio was not working, even with WSL IP 172.X.X.X.

    Using IPV6 localhost ::1 works like a charm!
    image

  10. angrygorilla commented on Nov 17, 2020

    @angrygorilla

    As doing a search on a similar issue brought me here

    The workaround [::1] works for ipv6-enabled app, but if you need ipv4, an option is to use the WSL2 subsystem ip adress instead, i.e. the adress returned by doing a ifconfig inside wsl.

    
    $ ifconfig
    eth0: flags=4163  mtu 1500
    [...]
            inet **172.19.171.88** netmask 255.255.240.0  broadcast 172.19.175.255
    [...]
    

    Still quite inconvenient as I think this address changes at every restart of WSL.

    thank you this was helpful.....

  11. afcruzs commented on May 16, 2021

    @afcruzs

    Has anybody make this work on Ubuntu-20.04?

  12. tristan-curve commented on May 21, 2021

    @tristan-curve

    Has anybody make this work on Ubuntu-20.04?

    Same issue as well for me on Ubuntu-20.04 can't connect to docker with any of the suggestions above

  13. kAlvaro commented on May 21, 2021

    @kAlvaro

    The IPv6 workaround (::1) works fine for me on Ubuntu 20.04 LTS.

  14. beriloqueiroz commented on May 28, 2021

    @beriloqueiroz

    Massimo Gengarelli (@massix)
    run the command on ubuntu:
    ip addr | grep -E "\binet.global" | sed -E "s/inet(.)/./\1/"

    result example:
    172.29.51.119
    172.17.0.1
    172.18.0.1

    get the first -> 172.29.51.119

    run the command in the shell windows as an administrator (the port, for example, is 3333):
    netsh interface portproxy add v4tov4 listenport=3333 listenaddress=0.0.0.0 connectport=3333 connectaddress=172.29.51.119

    listenport is the windows port
    listenaddress is the windows address, usually 0.0.0.0
    connectport is the ubuntu port
    connectaddress is the int ip ubuntu

  15. 27 remaining items

  16. jwhipp commented on Jul 21, 2023

    @jwhipp

    Any updates on this? I've tried the pre-release (1.3.14) to no avail. This complicates using WSL for development with Docker Desktop.

  17. usikpavel commented on Sep 4, 2023

    @usikpavel

    Just bumped into this problem.

    Impossible to start my Docker container on Windows:

    C:\Users\gusta> docker start 37ad37d59367b1978d7c8e823be19dbf2a2b48f9d798868bc09d59971085b1ec
    Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:50922 -> 0.0.0.0:0: listen tcp 0.0.0.0:50922: bind: An attempt was made to access a socket in a way forbidden by its access permissions.
    Error: failed to start containers: 37ad37d59367b1978d7c8e823be19dbf2a2b48f9d798868bc09d59971085b1ec
    

    I had the same problem.

    (I'm not sure, but I think, that) Solution for me was to check and disable some "unplugged" virtual Network Adapters.

    Details.

    1. I had Windscribe VPN on the host. When installed, it creates several virtual adapters (Windscribe VPN, Windscribe Windtun420). I've disabled both, and problem was sovled.
    2. On the other hand, I have several disconnected OpenVPN adapters. They don't affect this problem.
  18. kanine commented on Sep 19, 2023

    @kanine

    nervousapps re:fix Thank you so much, I knew there had to be a firewall issue, the documentation did not mention the two firewall rules you added.

    Perhaps this needs updating: https://learn.microsoft.com/en-us/windows/wsl/networking#accessing-a-wsl-2-distribution-from-your-local-area-network-lan

    The other error in that page is it says to use wsl hostname -i to get the WSL Host IP but it should say wsl hostname -I

    Note: I found this looking for a solution to how to connect to your Docker Apps over the LAN with WSL. I was connecting from the host just fine, it was specifically over LAN that was my issue until now! Thanks!

  19. themustafaomar commented on Sep 23, 2023

    @themustafaomar

    After a whole day of pulling hair out, solved my issue (in my case it was a Vite project) by switching from this:

    "scripts": {
        "dev": "vite",
        ...
    },
    

    To this:

    "scripts": {
        "dev": "vite --host 0.0.0.0",
        ...
    },
    

    Now I can access the project in the browser using http://localhost:5173 as normal.

    Docker command:

    docker run -d -p 5173:5173 --name vue-app-container vue-app
  20. sarthikg commented on Sep 24, 2023

    @sarthikg

    After a whole day of pulling hair out, solved my issue (in my case it was a Vite project) by switching from this:

    "scripts": {
        "dev": "vite",
        ...
    },
    

    To this:

    "scripts": {
        "dev": "vite --host 0.0.0.0",
        ...
    },
    

    Now I can access the project in the browser using http://localhost:5173 as normal.

    Docker command:

    docker run -d -p 5173:5173 --name vue-app-container vue-app

    This worked like a charm. Change the app host to bound to 0.0.0.0 instead of the default localhost or 127.0.0.1.

  21. robinloxley1 commented on Oct 31, 2023

    @robinloxley1

    bind your docker app host to 0.0.0.0, other than 127.0.0.1 works to me.
    wsl: ubuntu
    windows: 11
    docker image tested: mlflow docker run -it --rm -p 5000:5000 ghcr.io/mlflow/mlflow mlflow ui -h 0.0.0.0
    result: from windows browser, I can access locahost:5000

  22. deadcoder0904 commented on Feb 15, 2024

    @deadcoder0904

    this worked for me. why is microsoft so useless in trying to fix such a basic issue?

    its been 4 years guys. common now.

  23. General4077 commented on Feb 15, 2024

    @General4077

    Saw the same issue on Windows 10 with wsl Ubuntu-20.04. This doc solved for me. it boils down to the first command of nervousapps answer

  24. ismaelhamed commented on Mar 7, 2024

    @ismaelhamed

    Much of the networking problems I've encountered with WSL2 and Docker for Windows over the years went away when I switched to Mirrored mode networking.

    [wsl2]
    networkingMode=mirrored
    dnsTunneling=true
    autoProxy=true
    

    Then, you can, for instance, access your Windows host from Linux using 127.0.0.1. Additionally, dnsTunneling and autoProxy can also help if you're behind a corporate proxy.

  25. evilmonkey19 commented on Apr 24, 2024

    @evilmonkey19

    This bug is really annoying! 😠 Common, it's only Windows the only who do this. It should imitate Linux but it doesn't in this way.

    Anyway, for the folks who are developing on python i suggest the following:

    1. There is library that you can use to detect when you are in docker called detect: https://pypi.org/project/detect/
    2. In the standard python library there is a library called platform
    3. If you want to detect when using docker in WSL2 you can the following:
    import platform
    import detect
    
    if detect.docker and "WSL2" in platform.release():
       # Bind to 0.0.0.0 instead of 127.0.0.1

    Please Microsoft (@microsoft) solve this bug. A lot of people is waiting for you and we don't like to be fooled.

  26. happyday326 commented on Aug 29, 2024

    @happyday326

    I am using windows docker desktop v4.33.1. There's a feature in settings > features in development > beta features >enable host networking. It works for me with the feature enabled.

  27. Bugsbugme commented on Sep 11, 2024

    @Bugsbugme

    Massimo Gengarelli (@massix) run the command on ubuntu: ip addr | grep -E "\b_inet.global" | sed -E "s/inet(.)/._/\1/"

    result example: 172.29.51.119 172.17.0.1 172.18.0.1

    get the first -> 172.29.51.119

    run the command in the shell windows as an administrator (the port, for example, is 3333): netsh interface portproxy add v4tov4 listenport=3333 listenaddress=0.0.0.0 connectport=3333 connectaddress=172.29.51.119

    listenport is the windows port listenaddress is the windows address, usually 0.0.0.0 connectport is the ubuntu port connectaddress is the int ip ubuntu

    I know this is super old lol, but this fixed my problem!
    My problem is I have been trying to run a Plex server in a docker container, inside WSL Ubuntu. I wasn't able to enable remote access because my router obviously can only route traffic to my Windows host, but then Windows couldn't route the traffic to the docker container. (at least that's how I see it in my head, I don't know if that's accurate 😅)

    This seems like port forwarding, but between Windows and the docker container. Which makes sense. I figured it would be something like that but didn't know how to actually do it.

  28. craigloewen-msft commented on May 27, 2025

    @craigloewen-msft
    Member

    Hi folks, closing out old issues. I don't think this is reproing anymore, and if you're still seeing this please file it as a new issue. Thank you!

  29. ChristianOConnor commented on Jul 7, 2025

    @ChristianOConnor

    I created an app with Visual Studio. Everything I did was an out-of-the-box selection. I picked ASP.NET Core Web App (Model-View-Controller) > Framework: .NET 8.0 (Long Term Support), ✔️Enable Container Support, Container OS: Linux, Container build type: Dockerfile` and created the project:

    enter image description here

    enter image description here

    I have Docker Desktop running on Windows 11 with WSL2. It works perfectly on several other docker projects I've tested.

    When I try to run the project in Visual Studio by clicking ▶️ Container (Dockerfile), it fails to connected with the browser (in this case Edge):

    enter image description here

    It's extremely vanilla and it won't work out of the box on a 100% up to date Windows/Docker system...

    I am pretty sure the error is the ports not being properly routed to the Windows host from WSL2. I tried turning off WSL2 in Docker Desktop and instead defaulting to Hyper-V and then it worked perfectly with the exact same project and configuration.

    I could just use Hyper-V but I would rather use WSL2 as many of the other Docker projects I run locally just use WSL2 Docker Desktop and I don't want to have to keep switching back and forth.

    This is the output of my container logs:

    PS C:\Users\MYUSERNAMEHERE> docker logs WebMVCTestContain1 --tail 20
    warn: Microsoft.AspNetCore.DataProtection.Repositories.EphemeralXmlRepository[50]
          Using an in-memory repository. Keys will not be persisted to storage.
    warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[59]
          Neither user profile nor HKLM registry available. Using an ephemeral key repository. Protected data will be unavailable when application exits.
    warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
          No XML encryptor configured. Key {GUID-LOOKING-STRING-HERE} may be persisted to storage in unencrypted form.
    info: Microsoft.Hosting.Lifetime[14]
          Now listening on: http://[::]:8080
    info: Microsoft.Hosting.Lifetime[14]
          Now listening on: https://[::]:8081
    info: Microsoft.Hosting.Lifetime[0]
          Application started. Press Ctrl+C to shut down.
    info: Microsoft.Hosting.Lifetime[0]
          Hosting environment: Development
    info: Microsoft.Hosting.Lifetime[0]
          Content root path: /app
    PS C:\Users\Christian>
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions