# Accessing host machine from within docker container

**URL:** <https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248>\
**Category:** Docker Desktop\
**Tags:** docker, macos\
**Created:** [May 31, 2016, 10:42pm UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248 "2016-05-31T22:42:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayre](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@jayre](https://forums.docker.com/u/jayre)\
**Post date:** [May 31, 2016, 10:42pm UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/1 "2016-05-31T22:42:05Z")

</div>

### 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

### Information

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

---

<div class="post-metadata">

**Author:** ![keloran](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/keloran/32/7986_2.png) [@keloran](https://forums.docker.com/u/keloran)\
**Post date:** [June 1, 2016, 7:43am UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/2 "2016-06-01T07:43:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jayre](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@jayre](https://forums.docker.com/u/jayre)\
**Post date:** [June 1, 2016, 10:34am UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/3 "2016-06-01T10:34:08Z")

</div>

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

Rails  
–postgres  
–rabbitmq  
–looker  
----mysql

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.

---

<div class="post-metadata">

**Author:** ![garadox](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/garadox/32/8688_2.png) [@garadox](https://forums.docker.com/u/garadox)\
**Post date:** [June 2, 2016, 8:50pm UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/4 "2016-06-02T20:50:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mikehaertl](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/mikehaertl/32/11000_2.png) [@mikehaertl](https://forums.docker.com/u/mikehaertl)\
**Post date:** [June 4, 2016, 8:50am UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/5 "2016-06-04T08:50:22Z")

</div>

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 192.168.0.0/24 --gateway 192.168.0.1 dockernet

```

Now each container can connect to the host under the fixed IP `192.168.0.1`.

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'
services:
    db:
        image: some/image
        networks:
            - dockernet
networks:
    dockernet:
        external: true

```

Networks are described in the [network documentation](https://docs.docker.com/engine/userguide/networking/dockernetworks/). They are quite useful and not very hard to understand.

---

<div class="post-metadata">

**Author:** ![ivstas](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/ivstas/32/11033_2.png) [@ivstas](https://forums.docker.com/u/ivstas)\
**Post date:** [June 7, 2016, 4:03pm UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/6 "2016-06-07T16:03:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![drngo](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/drngo/32/11100_2.png) [@drngo](https://forums.docker.com/u/drngo)\
**Post date:** [June 7, 2016, 9:10pm UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/7 "2016-06-07T21:10:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ruffsl](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/ruffsl/32/7649_2.png) [@ruffsl](https://forums.docker.com/u/ruffsl)\
**Post date:** [March 24, 2017, 2:35am UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/8 "2017-03-24T02:35:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![strayerror](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@strayerror](https://forums.docker.com/u/strayerror)\
**Post date:** [April 16, 2017, 11:54pm UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/9 "2017-04-16T23:54:11Z")

</div>

I recently created a [repo](https://github.com/mal/docker-for-mac-host-bridge) 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.

---

<div class="post-metadata">

**Author:** ![fleonetti](https://avatars.discourse-cdn.com/v4/letter/f/ecc23a/32.png) [@fleonetti](https://forums.docker.com/u/fleonetti)\
**Post date:** [April 17, 2017, 6:36am UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/10 "2017-04-17T06:36:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rhudson](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/rhudson/32/15940_2.png) [@rhudson](https://forums.docker.com/u/rhudson)\
**Post date:** [May 9, 2017, 3:09pm UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/11 "2017-05-09T15:09:55Z")

</div>

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 }'
```

---

<div class="post-metadata">

**Author:** ![ashwinks](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@ashwinks](https://forums.docker.com/u/ashwinks)\
**Post date:** [May 12, 2017, 8:30am UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/12 "2017-05-12T08:30:39Z")

</div>

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

```auto
docker run --add-host="localhost:192.168.65.1"

```

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

---

<div class="post-metadata">

**Author:** ![danj210](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/danj210/32/17005_2.png) [@danj210](https://forums.docker.com/u/danj210)\
**Post date:** [June 15, 2017, 6:14pm UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/13 "2017-06-15T18:14:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jjzabkar](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/jjzabkar/32/16768_2.png) [@jjzabkar](https://forums.docker.com/u/jjzabkar)\
**Post date:** [August 8, 2017, 3:22pm UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/14 "2017-08-08T15:22:46Z")

</div>

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

`sudo ifconfig lo0 alias 172.16.222.111`

Then in `docker run`, I used:

`--add-host my.private.dnsname.com:172.16.222.111`

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

---

<div class="post-metadata">

**Author:** ![kevinpmcc](https://avatars.discourse-cdn.com/v4/letter/k/bbe5ce/32.png) [@kevinpmcc](https://forums.docker.com/u/kevinpmcc)\
**Post date:** [September 29, 2017, 10:05pm UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/15 "2017-09-29T22:05:05Z")

</div>

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

from [Docker for Mac release notes](https://docs.docker.com/docker-for-mac/release-notes/#docker-community-edition-17060-ce-mac18-2017-06-28-stable)

> 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

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

```

---

<div class="post-metadata">

**Author:** ![rohaq](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/rohaq/32/17799_2.png) [@rohaq](https://forums.docker.com/u/rohaq)\
**Post date:** [November 8, 2017, 2:53am UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/16 "2017-11-08T02:53:59Z")

</div>

There’s also docker.for.win.localhost 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 - docker.for.win.localhost will mean nothing to half of them. Any suggestions?

---

<div class="post-metadata">

**Author:** ![luiszambrano](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@luiszambrano](https://forums.docker.com/u/luiszambrano)\
**Post date:** [November 30, 2017, 3:17pm UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/17 "2017-11-30T15:17:04Z")

</div>

> <https://gist.github.com/LuisErnestoZamb/f0c60859b2056ad8f5a59ae42631abf9>

---

<div class="post-metadata">

**Author:** ![stephanegerber](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@stephanegerber](https://forums.docker.com/u/stephanegerber)\
**Post date:** [February 17, 2018, 7:18pm UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/19 "2018-02-17T19:18:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sdetweil](https://avatars.discourse-cdn.com/v4/letter/s/e19b73/32.png) [@sdetweil](https://forums.docker.com/u/sdetweil)\
**Post date:** [February 17, 2018, 11:32pm UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/20 "2018-02-17T23:32:24Z")

</div>

i would use the --add\_host parm when u start the container

---

<div class="post-metadata">

**Author:** ![felipegouveiae](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/felipegouveiae/32/19531_2.png) [@felipegouveiae](https://forums.docker.com/u/felipegouveiae)\
**Post date:** [May 27, 2018, 7:53pm UTC](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/21 "2018-05-27T19:53:48Z")

</div>

no network nor hosts are needed. As @rohaq and @kevinpmcc mentioned,

for windows : docker.for.win.localhost  
for mac : docker.for.mac.localhost

It works like a charm for me.

Thanks @rohaq and @kevinpmcc !

[Next page](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248.md?page=2)
