0

i am trying to setup a node + react app for local develpoment environment using docker + nginx

nginx is running on 3002

  1. for route /api strip off the /api and redirect to service api i.e. the node app
  2. redirect other routes to service client i.e. react app

docker-compose file

version: "3"

services:
  api:
    build:
      context: api
      dockerfile: Dockerfile.dev
    ports:
      - 3000:3000
    volumes:
      - ./api:/app

  client:
    stdin_open: true
    build:
      context: client
      dockerfile: Dockerfile.dev
    ports:
      - 3001:3000
    volumes:
      - ./client:/app

  nginx:
    restart: always
    build:
      context: nginx
      dockerfile: Dockerfile.dev
    ports:
      - 3002:80
    volumes:
      - ./nginx/default.dev.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - client
      - api

nginx default.dev.conf

upstream api {
    server api:3000;
}

upstream client {
    server client:3001;
}

server {
    listen 80;

    location / {
        proxy_pass http://client;
    }

    location /sockjs-node {
        proxy_pass http://client;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

    location /api {
        rewrite /api/(.*) /$1;
        proxy_pass http://api/;
    }
}

client Dockerfile.dev

FROM node:alpine

WORKDIR /app

CMD ["npm", "run", "start"]

api Dockerfile.dev

FROM node:alpine

WORKDIR /app

CMD ["npm", "run", "dev"]

nginx Dockerfile.dev

FROM nginx
  1. on visiting http://localhost:3002/api - works
  2. on visiting http://localhost:3002 - i get 502 Bad Gateway
  3. http://localhost:3000 and http://localhost:3001 work
  4. on docker exec -it bytecode_api_1 sh (i.e. going inside terminal of api container) ping client and ping nginx work
  5. the issue is in the connection between reat-app and nginx i.e. neither is reqeust from react going to nginx and nor is a request to http://localhost:3002 being directed to react-server

EDIT: figured it out with the help of some good people on discord, i had made a silly mistake, it should be

upstream client {
    server client:3000;
}

i.e. the container port

keemahs
  • 780
  • 11
  • 14
  • 1
    Why so over "complicated" with docker? Install node.js & nginx local on your development machine. Done. – Marc Feb 07 '21 at 00:17
  • thanks @Marc i thought because i want to make a microservice kind of architecture multiple servers and client and using docker would become very easy for e.g. i can use just /api for api service, /auth for auth service etc in the react app and the main reason is i wanted to allow compiling code for different languages, so i though i might be able to achieve thisusing docker – keemahs Feb 07 '21 at 00:30
  • 1
    When I run your docker compose I get `npm ERR! path /app - npm ERR! command failed - npm ERR! command sh -c react-scripts start` – 0xMH Feb 07 '21 at 00:57
  • oh i dont get that error...why ? – keemahs Feb 07 '21 at 00:59

0 Answers0