Accessing host machine as localhost from a Docker container that's also inside a boot2docker vm

前端 未结 3 2117
清歌不尽
清歌不尽 2020-12-02 08:43

Suppose I have a server running on port 8000 on OSX. How can my Docker container access it via localhost:8000? I can\'t change the hostname too as the app in th

相关标签:
3条回答
  • 2020-12-02 08:51

    Thanks to palimpestor's answer I figured it out:

    Instead of --net="host", use --add-host="localhost:10.0.2.2"

    Indeed:

    • 10.0.2.2 is the default gateway defined for the guest network interface in NAT mode (reference).
      Read: it's your host, seen from boot2docker.
    • --add-host... is adding localhost 10.0.2.2 in /etc/hosts (reference)

    Note: you need to have set up a NAT adapter in your boot2docker VM VirtualBox settings (I did it through the GUI, don't know the CLI).

    0 讨论(0)
  • 2020-12-02 08:52

    If I understand your intent, it is to: Connect from a container to a host machine port

    As of 18.3, the docker team has us covered:

    Us the alias host.docker.internal

    I WANT TO CONNECT FROM A CONTAINER TO A SERVICE ON THE HOST The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker Desktop for Windows.

    The gateway is also reachable as gateway.docker.internal.

    https://docs.docker.com/docker-for-windows/networking/

    0 讨论(0)
  • 2020-12-02 09:16

    Instead of running with --net="host", try --add-host="localhost:192.168.59.3", which is the boot2docker host IP.

    0 讨论(0)
提交回复
热议问题