ashwinks
(Ashwinks)
May 12, 2017, 8:30am
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: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 }'
)
6 Likes
danj210
(Danj210)
June 15, 2017, 6:14pm
13
Not a helpful question at all. I am trying to do the same thing as original poster.
jjzabkar
(Jjzabkar)
August 8, 2017, 3:22pm
14
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.
2 Likes
kevinpmcc
(Kevinpmcc)
September 29, 2017, 10:05pm
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;
13 Likes
rohaq
(Rohaq)
November 8, 2017, 2:53am
16
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?
4 Likes
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.
sdetweil
(Sam)
February 17, 2018, 11:32pm
20
i would use the --add_host parm when u start the container
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 !
2 Likes
What if your docker works with linux?
qoomon
(Qoomon)
August 6, 2018, 8:25am
23
Docker Container to access host ports => https://github.com/qoomon/docker-host .
clns
(Clns)
October 31, 2018, 8:58pm
25
Yes, same issue here and a valid use case.
kensai
(Kensai)
November 24, 2018, 12:27am
27
As per this page you can also use host.docker.internal
as a platform-agnostic way to reference the host.
1 Like
vaelek
December 6, 2018, 6:37pm
28
I thought you were going to be my hero… but as per the very next sentence on that page,
This is for development purpose and will not work in a production environment outside of Docker for Windows.
EDIT: I see the same comment in the Mac docs saying the same thing, all as of 18.03, however I have 18.07 and that does not resolve.
ognyandim
(Ognyan Dimitrov)
January 16, 2019, 8:51am
29
Can you use environment variables for that matter? You can write any host information (the host ip) in a variable and pass it to the container the moment you run it.
thank you ! you saved my week <3
jason5lee
(Jason5lee)
June 10, 2021, 10:47am
32
My situation is to use the host HTTP Proxy in order to access the outer network for the build task.
avbentem
(Arjan)
June 10, 2021, 12:14pm
33
Aside, for Linux this seems to require --add-host=host.docker.internal:host-gateway
. Or use 172.17.0.1
instead of host.docker.internal
.