Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection Refused from Request Inside Docker Compose

I have an API running on my host machine on port 8000. Meanwhile, I have a docker compose cluster with one container that's supposed to connect said API. To get the url for the request, I use "host.docker.internal:8000" on my windows machine and it works wonderfully. However, I have a linux deployment server and in there, "host.docker.internal" doesn't resolve to anything, causing a connection error to the API. I saw on another post on stackoverflow, that you solve this on linux by adding the following on your docker-compose.yaml

services:
  service_name:
    extra_hosts:
      - host.docker.internal:host-gateway

This added the docker0 IP to /etc/hosts, but when I try to do a GET request, the resulting message is:

Failed to connect to host.docker.internal port 8000: Connection refused

I'm really confused right now. I don't know if this is a firewall issue, a docker issue, a docker compose issue, a docker on linux issue. Please help...

like image 974
Eddysanoli Avatar asked Feb 04 '26 20:02

Eddysanoli


1 Answers

Credit to @Hans Kilian:

  • Add extra_hosts to docker-compose file
  • Change URL to use host.docker.internal instead of localhost
  • Change service to serve on 0.0.0.0 instead of localhost
like image 65
A.Wan Avatar answered Feb 06 '26 11:02

A.Wan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!