skip to Main Content

I run nodejs on localhost while nginx runs as a docker container with docker-compose

I can’t make any connections to the node js app:

nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    server {
        listen 80;
        server_name licensing.cluster-ops.co;
        location / {
                proxy_pass http://md_license_server:3005;
                proxy_set_header Host $host;
        }
    }
}

docker-compose.yml

version: "3.3"
services:
  md_license_server:
   image: root/md-license-server
   expose:
    - 3005
   ports:
    - 3005:3005
  nginx:
   image: nginx:latest
   container_name: nginx
   ports:
    - 80:80
    - 443:443
   volumes:
    - /etc/ssl/certs:/etc/ssl/certs
    - /etc/ssl/private:/etc/ssl/private
    - /root/docker/nginx.conf:/etc/nginx/nginx.conf


I tried everything out there.

extra_hosts:
– "host.docker.internal:127.0.0.1"

^this won’t work.

The node app works perfectly fine on localhost.. I tried to make somem curl calls and the response was ok.

This is the error message i get from docker:

2021/12/06 14:29:06 [error] 24#24: *3 connect() failed (111: Connection refused) 
while connecting to upstream, client: 139.59.147.204, server: 
localhost, request: "GET /ping HTTP/1.1", upstream: "http://172.19.0.2:3005
/ping", host:...

It looks like "172.19.0.2:3005" is either the IP of the md_license service.

running

docker-compose exec md_license_server curl http://localhost:3005/ping returns true

which means the node server works within the container.


I feel this is something about nginx.

2

Answers


  1. Most probably you have IP range overlap between docker network & host network. But let’s not assume that now :


    • If you are using Docker for desktop (Windows, Mac) , No need to add extra hosts. Directly:

      upstream backend {
        server host.docker.internal:3005;
      }
      
    • If you are NOT using Docker for Desktop (Linux), you need to check the private IP of your host hostname -i, then put that private IP in the nginx config

      # assuming that `hostname -i` prints 192.168.x.y
      upstream backend {
        server 192.168.x.y:3005;
      }
      

    Now if you want to put that dynamically, this is another topic, but you could check nginx templates.


    Returning back to the 1st assumption:
    whatever the case , if there is overlap between Docker network and your Host network, the router will not forward the packet to the host and it keep being inside docker network. So let’s check:

    • Check your Host IP : hostname -i
    • Check your Docker container IP : docker-compose exec nginx hostname -i

    IF there is overlap, you will need to provide custom docker network:

    services:
       nginx:
         image: ....
         networks:
           custom-net: # attach nginx container to that network
             ipv4_address: 172.24.0.22 # assign static IP for your container from the network defined below.
    
    networks:
      custom-net: # create new docker network
        ipam:
          config:
           - subnet: 172.24.0.0/24 # with explicit CIDR range
    
    Login or Signup to reply.
  2. It was a problem caused by fastify.

    Solution can be found here: https://github.com/fastify/fastify/issues/935

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search