Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to service in the host machine from inside a docker container?

I have a docker daemon started in the host machine listen to some ip address and port, say 10.10.10.10, and port 1234, then I start a container by invoking

sudo docker -H 10.10.10.10:1234 centos /bin/bash

Meanwhile, I have a web service runnig on the host machine, running on the port 8080. Then from inside the container, I cannot connect to this server. I tried

curl http://10.10.10.10:8080

but got an error message:

curl: (7) couldn't connect to host

But I can access the server in other machines, like http://10.10.10.11:8080 It seems that docker container cannot access the service in its own host machine? Is there anyway to fix this? Thanks

like image 758
yuan Avatar asked Apr 13 '15 05:04

yuan


1 Answers

That was discussed in issue 1143 and there might be one day a --link-host option.

In the meantime, the blog post "Accessing the Docker Host Server Within a Container" lists a few option:

  • The Gateway Approach: netstat -nr | grep '^0\.0\.0\.0' | awk '{print $2}'
  • The IP Approach: boot2docker ip address is 192.168.59.103.

Although there’s no way to introspect the host’s ip address (AFAIK) you can pass this in via an environment variable:

docker@boot2docker:~$  docker run -i -t -e DOCKER_HOST=192.168.59.103 ubuntu /bin/bash
root@07561b0607f4:/# env
HOSTNAME=07561b0607f4
DOCKER_HOST=192.168.59.103

You have other options in "Network Configuration", with the virtual interface named docker0 on the host machine.

like image 107
VonC Avatar answered Sep 21 '22 02:09

VonC