How to get Docker Server Node Js service working

Viewed 17

I'm having trouble getting my docker node js back-end server working.

Here's my docker-compose file

version: '3.7'
services:
  mysql_db:
    image: mysql
    container_name: mysql_container
    restart: always
    cap_add:
      - SYS_NICE
    volumes:
      - ./data:/var/lib/mysql
    ports:
      - "3306:3306"
    env_file:
      - .env
    environment:
      MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
      MYSQL_HOST: "${MYSQL_HOST}"
      MYSQL_DATABASE: "${MYSQL_DATABASE}"
      MYSQL_USER: "${MYSQL_USER}"
      MYSQL_PASSWORD: "${MYSQL_PASSWORD}"
    networks:
      - react_network

  jobs:
    image: username/image:latest
    restart: always
    ports:
      - "8000:80"
    container_name: jobs_container
    volumes:
      - /app/node_modules
      - ./:/app
    stdin_open: true
    tty: true

  api:
    restart: always
    image: username/image:latest
    container_name: server_container
    ports:
      - "3001:80"
    env_file:
      - .env
    depends_on:
      - mysql_db
    environment:
      MYSQL_HOST_IP: mysql_db
    networks:
      - react_network

  client:
    image: username/image:latest
    restart: always
    stdin_open: true
    tty: true
    container_name: client_container
    ports:
      - "3000:80"
    networks:
      - react_network


volumes:
  data:

networks:
  react_network:

api is the name of my back-end node js service that I'm trying to get to work.

Here's my index.js file from the server directory

const express = require('express');
const app = express();
const cors = require('cors');
const bodyParser = require('body-parser');
const mysql_connection = require("./mysql.conf");
const nodemailer = require('nodemailer');
require('dotenv').config();

app.use(cors());

// support parsing of application/json type post data
app.use(bodyParser.json());

//support parsing of application/x-www-form-urlencoded post data
app.use(bodyParser.urlencoded({ extended: true }));

app.get('/test', (req, res) => {
    res.send("This is a test.");
});

app.listen('3001', () => {
    //console.log('Listening on port 3001');
});

When I make a GET request using postman to https://example.com/api/test, I get a 502 Bad Gateway.

Here's my example.com configuration file located in /etc/nginx/sites-available

server {
   listen 443 ssl;
   server_name example.com;

   ssl_certificate /etc/ssl/example.com/certificate.crt; 
   ssl_certificate_key /etc/ssl/example.com/private.key;

   location / {
      proxy_pass http://localhost:3000; #This is the client service
   }

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

    location /api {
       rewrite /api/(.*) /$1 break;
       proxy_pass http://localhost:3001; #This is the api service
   }
}

This is my Dockerfile in the server directory

FROM node:14-alpine

WORKDIR "/app"
COPY package.json ./
COPY yarn.lock ./
#RUN yarn cache clean && yarn --update-checksums
COPY ./ ./
RUN yarn install
EXPOSE 3001
CMD ["yarn", "run", "start"]

Here's my directory structure

enter image description here

I'm thinking that my problem is in the configuration file something about this:

location /api {
           rewrite /api/(.*) /$1 break;
           proxy_pass http://localhost:3001; #This is the api service
       }

could be the problem?

I should also mention that I have installed nginx in my host machine. I'm not using an nginx docker image... no reason just how I have it setup.

0 Answers
Related