Docker Community Forums

Share and learn in the Docker community.

Accessing host machine from within docker container


(Jayre) #1

Expected behavior

I should have a way to access host machine from within container.

Actual behavior

Cant figure out how to do that with docker for mac beta


With the change to all the docker images running on localhost and the ditching of virtualbox (which from what little I understand, made networking more difficult), I would think there would be a standard way to access the host machine from within the docker container. Is there a way to do this with docker for mac? All the docs regarding doing so seem to only be relevant to the virtual box version and docker-machine

Cannot access network port on host using bridge network
(Max Hooton) #2

I think i must be missing something here, why does anyone want to access the host machine

if you need to talk to other containers, then you setup a link between,

but why do you need / want to access the root machine, i can’t see a purpose for it

(Jayre) #3

In my case, I am running a data visualization app (looker), which needs to whitelist the url of the main application, to allow embedding of iframes under that ip/domain. So in development, the iframes don’t resolve, because there is no way for looker to know the ip of the host machine which is running my rails app.

Basically the dependency stack is


Links won’t work because I, like many others from what I could gather on these boards, don’t want to run the main application (rails) in a docker container, in development. It will only make development/debugging more difficult.

(Gareth Harcombe-Minson) #4

An example would be running an nginx container, which reverse proxies requests back to the host for a web application.

The web application is not in a container because it’s a heavy grails app with multiple dependencies, and will be a container eventually but it’s not currently.

(Mikehaertl) #5

The main problem for me was to reliably find out the IP address of the host. So in order to have a fixed set of IPs for both my host and my containers I’ve set up a docker network like this:

docker network create -d bridge --subnet --gateway dockernet

Now each container can connect to the host under the fixed IP

You just need to make sure, that you connect all your containers to that “dockernet” network you just created. You can do that with the --net=dockernet option for docker run. Or from a docker-compose.yml:

version: '2'
        image: some/image
            - dockernet
        external: true

Networks are described in the network documentation. They are quite useful and not very hard to understand.

(Stanislav Ivanov) #6

It will be possible to ping host by gateway ip, but curl gateway-ip-addr still does not work in container.

(Drngo) #7

Another example would be running a php application that has xdebug set up on it. In order to debug, the server must connect to the ide with an ip address that is defined in the configuration. At the moment, I have to hardcode the ip address of the host machine into that configuration file.

(Ruffsl) #8

I’m having a similar issue: can ping the host, yet can not curl any data from the host.

(Strayerror) #9

I recently created a repo to address the host<->container connectivity issues in Docker for Mac. It’s not an ideal solution by any stretch (it’s very much a hack), but it does suffice for local development. It does have limitations; it only supports a single named host network right now, and must be re-run each time the Docker daemon is restarted (check the Readme). When used in it’s default configuration running ifconfig on your host machine should reveal a new tap1 interface, the IP reported there may be used inside containers connected to named bridge network to access the host machine.

(francesco) #10

I have the same issue. With solution by @mikehaertl I can ping the host, but I can’t curl.
Any suggestion to make it work?

(Rhudson) #11

Try connecting the container to the ‘host’ network.
e.g. –

docker run --network host ...

Run this from within the container to get the IP of the host:

route | awk '/^default/ { print $2 }'

(Ashwinks) #12

In case if you just want to reach your host machine as ‘localhost’ add the following option to docker run

docker run --add-host="localhost:"

In my case is always the IP to the host ( output of : route | awk '/^default/ { print $2 }' )

(Danj210) #13

Not a helpful question at all. I am trying to do the same thing as original poster.

(Jjzabkar) #14

Building on @mikehaertl’s response, on OSX, I was able to add an alias for my loopback adapter:

sudo ifconfig lo0 alias

Then in docker run, I used:


This workaround enables docker-to-localhost connectivity on OSX’s docker as of 17.06.

(Kevinpmcc) #15

You can now do this with docker for mac with docker.for.mac.localhost

from Docker for Mac release notes

Add an experimental DNS name for the host: docker.for.mac.localhost

In my nginx.conf I could reach localhost:8090 on the host machine from within a Docker container with

proxy_pass http://docker.for.mac.localhost:8090/app;

(Rohaq) #16

There’s also for Docker for Windows users.

What I’d really like though is a method to do this cross-platform - We have Linux and Windows users that use the same docker-compose.yaml - will mean nothing to half of them. Any suggestions?

(Luiszambrano) #17

(Stephanegerber) #19

Exactly, so far I am not able to achieve what seems an easy task. The goal beeing avoinding to put the ip in the image.

(Sam) #20

i would use the --add_host parm when u start the container