Docker Community Forums

Share and learn in the Docker community.

Accessing host machine from within docker container

docker
(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 }'
2 Likes
(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: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 }' )

5 Likes
(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 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.

7 Likes
(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;
28 Likes
(Rohaq) #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?

8 Likes
(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

(Felipegouveiae) #21

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
(Kostyavyrodov) #22

What if your docker works with linux?

1 Like
(Qoomon) #23

Docker Container to access host ports => https://github.com/qoomon/docker-host.

(Realdubb) #24

This worked ! Thank you

(Clns) #25

Yes, same issue here and a valid use case.

(Mosen11) #26

I try to access my mongoDB on host by the container but I get the following exception: Exception in monitor thread while connecting to server localhost:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:62)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:59)
at com.mongodb.connection.SocketStream.open(SocketStream.java:57)
I run my container with this command: docker run --network=“host” 659004210509. I try several variation but none of them worked.

(Kensai) #27

As per this page you can also use host.docker.internal as a platform-agnostic way to reference the host.

1 Like
#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.

(Ognyan Dimitrov) #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.

(Gary Forghetti) #30

Yes, check this post.