Connection refused using host.docker.internal in docker container

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.


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.