6

I have a localhost:8000 open on the host machine. Specifically:

$ php -S localhost:8000
PHP 7.1.19 Development Server started at Tue Jul 16 09:49:08 2019
Listening on http://localhost:8000

I can just do curl -i http://localhost:8000 on host to get the response, and I can also access it through host browser, no problem at all.

However, when I try to access host on docker container on my Mac, the connection refused:

$ curl -i http://host.docker.internal:8000
curl: (7) Failed to connect to host.docker.internal port 8000: Connection refused

$ telnet host.docker.internal 8000
Trying 192.168.65.2...
telnet: Unable to connect to remote host: Connection refused

The docker container can resolve the DNS host.docker.internal as can be seen from the telnet command and also I can ping it, but the connection is just refused.

$ ping host.docker.internal
PING host.docker.internal (192.168.65.2) 56(84) bytes of data.
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=1 ttl=37 time=0.186 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=2 ttl=37 time=0.463 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=3 ttl=37 time=0.420 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=4 ttl=37 time=0.428 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=5 ttl=37 time=0.722 ms

UPDATE

I found this GitHub issue comment, don't know if this helps.

1
  • What would happen if you have the webserver listen on 192.168.65.2 instead of localhost?
    – Konerak
    Jun 13, 2021 at 18:00

3 Answers 3

3

I had a similar problem and got it working by making sure that my tunnel accepted connections from everywhere.

Before:

ssh -N -L 5435:endpoint.rds.amazonaws.com:5432 [email protected]

After:

ssh -N -L 0.0.0.0:5435:endpoint.rds.amazonaws.com:5432 [email protected]
1
  • This! Same case here. The problem had nothing to do with Docker. The external service was not listening on all interfaces and therefore the container process could not reach it. Dec 14, 2023 at 11:41
1

I was able to get this to work. I am not sure if you are still facing this issue and my resolution will work for you. But, it is never a harm to try it out.

I noticed the containers that did not have a port to map/bind to the host has this issue of recognizing host.docker.internal to the host's localhost. Hence I used a dummy port for the container that has an issue connecting and it started to work.

Hope this helps.

2
0

Note: I posted this on SO (question 43879637) but I believe that question should be closed and reference this one since, in fact, it is not a programming issue.

Network Mode: Host

If network mode is host, there should be no problem, but then network isolation is not in place and each container is like a service in one server, they cannot share ports, they have all the same ip address, which is the address of the host.

In Docker Compose:

networks:
    my-network:
        driver: host

Network Mode: Bridge

A better approach is to use the bridge network, which emulates a local network where each container has its own IP address.

In Docker Compose:

networks:
    my-network:
        driver: bridge

If you have multiple bridge networks, sometimes iptables or route tables are not properly maintained by docker, as discussed in many articles / forums:

https://stackoverflow.com/questions/66067893/why-does-docker-give-error-connect-no-route-to-host-when-trying-to-connect-to

You will find that docker0 was the network interface linked to the first default bridge (172.17.0.0), and subsequent networks are associated to virtual devices such as br-d2938602303c, br-dfd44f0535bf...

Check changing iptables to accept incoming packets from all desired docker interfaces:

BEFORE

[root@my-server]# iptables -S INPUT
-P INPUT ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 8 -j REJECT --reject-with icmp-host-prohibited
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -i docker0 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8000:9999 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 2100:2199 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited

AFTER

# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -i docker0 -j ACCEPT
-A INPUT -i br-d2938602303c -j ACCEPT
-A INPUT -i br-dfd44f0535bf -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8000:9999 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2100:2199 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

Then restart ip tables service

service iptables restart

Then I also fixed the routing table, which looks like this

[root@jupiter ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         static.129.95.9 0.0.0.0         UG    0      0        0 eno1
static.129.95.9 0.0.0.0         255.255.255.255 UH    0      0        0 eno1
link-local      0.0.0.0         255.255.0.0     U     1002   0        0 eno1
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-d2938602303c
172.19.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-dfd44f0535bf

Useful links:

https://www.baeldung.com/linux/docker-connecting-containers-to-host

https://www.reddit.com/r/docker/comments/i5es7f/no_route_to_host_but_successful_google_ping/

https://stackoverflow.com/questions/66067893/why-does-docker-give-error-connect-no-route-to-host-when-trying-to-connect-to

https://stackoverflow.com/questions/43879637/docker-container-cannot-connect-to-host-machine-no-route-to-host/78041985#78041985

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .