38

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.

Paul Lam
  • 1,729
  • 1
  • 15
  • 25

3 Answers3

50

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

Igor Dubinskiy
  • 373
  • 1
  • 6
lajarre
  • 4,910
  • 6
  • 42
  • 69
  • I am not sure whats happening but this worked for me, I was trying to access local mongo in my container. – Abhay Kumar Jun 16 '16 at 16:26
  • 1
    For those using docker-compose.yml the equivalent of --add-host is the configuration option [extra_hosts](https://docs.docker.com/compose/compose-file/#extra_hosts) – Brandon Culley Sep 07 '17 at 15:15
  • 1
    That seems to be _adding_ a line to `/etc/hosts` - it does not override the existing line: `127.0.0.1 localhost` – benzaita Nov 28 '17 at 13:53
17

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

palimpsestor
  • 1,038
  • 1
  • 8
  • 28
  • just pinging 192.168.59.3 from inside the container don't seem to get a response – Paul Lam May 29 '15 at 22:44
  • 4
    During boot2docker initialization, boot2docker sets up two network adapters, one using NAT and the other a "host-only" adapter. The IP of the host on that host-only network is determined by virtualbox. You can inspect it by running `ifconfig` on your host and looking at the `vboxnet0` interface. By default it's 192.168.59.3, but you can change it in virtualbox's global settings at Preferences -> Network -> Host-only Networks. I am able to ping that IP both from within the boot2docker VM and from within a running docker container. – palimpsestor May 31 '15 at 04:48
5

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/

Randy Larson
  • 6,817
  • 2
  • 18
  • 13