3
votes

I'm running a multi-docker container locally with docker-compose, the containers are React front-end 'client', a Nodejs app 'api', and a Nginx proxy in sits in front of two. I have been using the docker-compose setup as follow for a while

version: '3'
services:
  client:
    build:
      dockerfile: Dockerfile.dev
      context: ./client
    volumes:
      - /usr/app/node_modules
      - ./client:/usr/app
  api:
    build:
      dockerfile: Dockerfile.dev
      context: ./server
    volumes:
      - /usr/app/node_modules
      - ./server:/usr/app
  nginx:
    restart: always
    build:
      dockerfile: Dockerfile.dev
      context: ./nginx
    ports:
      - '8080:80'

and my Nginx setup is as follows

upstream client {
    server client:3000;
}

upstream api {
    server api:5000;
}

server {
    listen 80;
    server_name _;

    location / {
        if ($http_x_forwarded_proto != 'https') {
            return 301 https://$host$request_uri;
        }
        proxy_pass http://client;
    }

    location /api {
        if ($http_x_forwarded_proto != 'https') {
            return 301 https://$host$request_uri;
        }
        rewrite /api/(.*) /$1 break;
        proxy_pass http://api;
    }
}

Recently when I tried to start up the containers, i got following error:

nginx_1   | 2019/08/08 18:11:12 [emerg] 1#1: host not found in upstream "client:3000" in /etc/nginx/conf.d/default.conf:2
nginx_1   | nginx: [emerg] host not found in upstream "client:3000" in /etc/nginx/conf.d/default.conf:2

Any idea why nginx not able to find upstream?

I have tried to add links to nginx setup blocks as follows:

  nginx:
    restart: always
    build:
      dockerfile: Dockerfile.dev
      context: ./nginx
    links:
      - client:client
      - api:api
    ports:
      - '8080:80'

I also tried 'depends_on' instead of links. After adding links, nginx no longer complains and exit with code 0. But when i visit the localhost:8080, it gives a 301 redirect to https://localhost.

Any help or direction are greatly appreciated!!!

1
Add a “depends_on” stanza to your nginx service to ensure that your upstream services are running / reachable before the nginx service starts, ref. stackoverflow.com/a/56976398/1423507masseyb

1 Answers

-1
votes

You should check names of your services. Docker compose will start your service api in pod named [YOUR_PROJECT_NAME]_api_1. Start only api and client and check output of docker ps. You should gey list of names of pods.

In newer docker_compose syntax versions you can use link_external to map [YOUR_PROJECT_NAME]_api_1 to api.