Accessing host machine service different in Docker Engine vs. Docker Desktop

29 Views Asked by At

I have a project that consists of two different services. The front-end, and the back-end API. The front-end exposes itself to the host on port 3000, and the backend on port 4000. For CORS to work, I have it setup so that accessing the front end from http://foo and the API from http://api.foo works flawlessly. I do this by configuring an nginx proxy service in the compose.yml for the backend which forwards requests on port 80 appropriately. The API network runs on the 170.18.0.0/24 network. My nginx config looks like this.

events {
  worker_connections 1024;
}

http {
  upstream api {
    server api:3000;
  }

  upstream frontend {
    server 170.18.0.1:3000;
  }

  server {
    listen 80;
    server_name ~^api\..+$;

    location / {
      proxy_pass http://api;
    }
  }

  server {
    listen 80;
    server_name ~^(?!api\.)[^.]+\.?.+$;

    location / {
      proxy_pass http://frontend;
    }
  }
}

By proxying the requests back to 170.18.0.1:3000 the other service is able to answer these requests and everything is good.

However, on Docker Desktop, the proxying to 170.18.0.1:3000 hangs, and then times out.

To troubleshoot, I made sure that the Docker container can ping the host.

$ ping 170.18.0.1
PING 170.18.0.1 (170.18.0.1): 56 data bytes
64 bytes from 170.18.0.1: seq=0 ttl=42 time=0.059 ms
64 bytes from 170.18.0.1: seq=1 ttl=42 time=0.325 ms
64 bytes from 170.18.0.1: seq=2 ttl=42 time=0.248 ms

But it cannot access the web service.

$ wget 170.18.0.1:3000
Connecting to 170.18.0.1:3000 (170.18.0.1:3000)
wget: can't connect to remote host (170.18.0.1): Operation timed out

There is a few minutes delay between "Connecting ..." and "wget: ...".

It appears Docker Engine and Docker Desktop have different behavior in this respect. It's a problem because, while I'm using Linux, we have developers running OSX and Windows. They don't have the option to use Docker Engine.

The only work-around we've found is to replace 172.18.0.1 with the user's actual IP address on their LAN. This requires manual work when setting up a dev environment, and creates a brittle setup where everything breaks when the user's IP address changes.

Does anyone know of a way to get the proxy to work in a stable way?

0

There are 0 best solutions below