Xiphias
Xiphias

Reputation: 4716

Cannot access ports on MacOSX host from within docker container

These were the steps I took:

Prework:

First:

docker-machine create -d virtualbox default

Then, I created a container with the following Dockerfile:

FROM centos:latest

Nothing else — just a copy of CentOS. I built the container:

docker build -t mycontainer .

And run it:

docker run -it --net="host" --name="test" -p 9200:9200 mycontainer

Problem: When I go inside the container and try to access a service running on MacOSX (such as a simple webserver or a local elasticsearch), I get:

curl localhost:9200
curl: (7) Failed connect to localhost:9200; Connection refused

I get the same error from within my docker vm (docker-machine ssh default).

I tried port forwarding in virtualbox, setting 9200 to 9200 — but it did not help.

Any ideas?

Upvotes: 2

Views: 1556

Answers (2)

dprothero
dprothero

Reputation: 2722

From within the container, simply reference:

host.docker.internal

So, your curl command would be:

curl http://host.docker.internal:9200

Upvotes: 0

christian
christian

Reputation: 10188

You cannot connect from a docker container to a port on your host with localhost:port (unless you run your container with --net="host" )

You need to specify the ip address of your host to connect.

Please check for the IP on the host:

dude-server:stackoverflow cwoehrle$ ping $(hostname)
PING dude-server (192.168.1.169): 56 data bytes
64 bytes from 192.168.1.169: icmp_seq=0 ttl=64 time=0.053 ms
64 bytes from 192.168.1.169: icmp_seq=1 ttl=64 time=0.069 ms

In the container (use your ip respectively):

root@8975fada1ac3:/# nc 192.168.1.169 9200

Edited: To connect to your host ports on a mac you can use the default gateway address 10.0.2.2, e.g. nc 10.0.2.2 9200

Upvotes: 4

Related Questions