Accessing host machine as localhost from a Docker

2019-01-12 22:33发布

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 the container is not in my control.

I've read this previous discussion on using --net="host" for a container to access the host machine's network. However, I'm on OSX and Docker runs inside a VM so localhost from the Docker container with --net="host" goes to the VM and not my real machine.

Then I tried port forwarding workaround like so: VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port8000,tcp,,8000,,8000"; to no avail.

Any suggestion would be greatly appreciated.

2条回答
\"骚年 ilove
2楼-- · 2019-01-12 23:16

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).

查看更多
疯言疯语
3楼-- · 2019-01-12 23:21

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

查看更多
登录 后发表回答