Skip to content

[WSL 2] can't access container in WSL from Windows #4170

Closed

Description

@markmail

Unable to access localhost from Windows

start container in WSL with

docker run -p 9001:8080 adminer

curl http://localhost:9001 returned with result no problem

however when I performed the same curl command from Windows land, the connection was refused

curl: (7) Failed to connect to localhost port 9001: Connection refused

check with netstat under Windows, and confirmed port 9001 is not visible from Windows

Microsoft Windows [Version 10.0.18917.1000]
Ubuntu 18.04.2 LTS
Docker version 18.09.6, build 481bc77

Ubuntu installed from Microsoft Store and converted to WSL 2 as per official Microsoft instruction
Docker installed as per official Docker guide for Ubuntu

Activity

  1. brdhunga commented on Jun 17, 2019

    @brdhunga

    You have to use the ip address of WSL 2 (as it is a hyper-v container). you can do so by running ip addr | grep eth0 within WSL 2 terminal, and you can use that ip address to access any port. You might have to play with the firewall too.

  2. onomatopellan commented on Jun 17, 2019

    @onomatopellan

    It will change with every new session but you can read the IP easily from task manager:

    Anotación 2019-06-18 003949

  3. markmail commented on Jun 18, 2019

    @markmail
    Author

    Isn't that obvious, haha.

    Thanks for pointing that out, solved my problem big time. I keep recalling someone from one of the WSL 2 presentation I watched saying that WSL 2 networking environment is the same as native Windows environment....etc perhaps he was referring to future release

  4. marioeu commented on May 9, 2020

    @marioeu

    Its worth to say that with WSL (version 1) localhost/127.0.0.1 works fine. But once updated to WSL 2, its really necessary to run with local eth0 IP address.
    And I agree with markmail... it isnt that obvious and must be highlighted somewhere...

  5. Antony-Wish commented on Sep 9, 2020

    @Antony-Wish

    The solution works for me.
    However, if I run a server in WSL2 instead of in docker container, I can access my service using 127.0.0.1.
    Why 127.0.0.1 can be used to access services that run in WSL2 and eth0 should be used to access service in docker container?

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