Reputation: 544
I'm runing my docker container with network host. I can ping my localhost with the ping localhost
command in the container and everything is fine. But I can't reach localhost:8000. Is there a way that I can reach localhost on specific port from docker container?
Here is what I'm trying to do:
I have an API on my host machine that is runing on port 8000. When I try to curl from my docker container to that api, I can't. This is why I changed my connection from bridge to host.
After I did that I was able to ping localhost (not sure which localhost) but I'm assuming it was the host machine. Curl returns me this error.
GuzzleHttp\Exception\ConnectException: cURL error 7: Failed to connect to localhost port 8000: Connection refused (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) in file /var/www/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php on line 200
When I try to tellnet localhost It doesn't work aswell. (can't resolve host or something like that). I'm doing everything from inside of the docker container as root.
My docker container is build on the following image php:7.2-apache
I set my connection back to bridnge and used the host.docker.internal
host that is in the documentation but there was no success. I can still ping from my container host.docker.internal
and get a response
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.771 ms 64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=2 ttl=37 time=1.20 ms 64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=3 ttl=37 time=0.548 ms
GuzzleHttp\Exception\ConnectException: cURL error 7: Failed to connect to host.docker.internal port 8000: Connection refused (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) in file /var/www/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php on line 200
I started my laravel api with php artisan serve
using the following parameters
php artisan serve --host=host.docker.internal --port=8000
I can access it from my browser
Laravel development server started on http://host.docker.internal:8000/ [Thu Jul 23 10:00:24 2020] 192.168.x.x:60126 [200]: /favicon.ico
But the docker container still can't access the server from the container.
Lastly I tried to access http://host.docker.internal with curl and it worked fine. The problem is in the port. Could it be firewall or something that is blocking it? I listed the 8000 port in my Windows on inbound rules for public, private and domain networks with TCP connections but that did not work aswell. I'm confused.
I turned the firewall off there is no success still
Problem solved
When I was doing the curl request I was trying to reach a port in the following way:
host.docker.internal:8000
- This was my curl URL leading to connectiong refused.
After that I check Guzzle if I can set somehow a port without specifing it in the url.
So I found this in the docs. I used that to specify my port 8000 and ran my artisan server with the following command
php artisan serve --host=host.docker.internal --port=8000
also I set my APP_URL
to APP_URL=http://host.docker.internal/
From there on everything worked like a charm.
Upvotes: 0
Views: 4221
Reputation: 1435
Look at this question How to access host port from docker container
if you are using linux the argument --net="host"
will work
Upvotes: 4