skip to Main Content

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…

2

Answers


  1. 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]
    
    Login or Signup to reply.
  2. 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
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search