Is this a problem with Nginx or Node.js?

Hello,
The Dockerfile is as follows:

FROM node:latest as build-stage
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package*.json /usr/src/app/package.json
RUN npm install
RUN npm update
COPY . /usr/src/app
EXPOSE 3000

#production stage
FROM nginx:latest as production-stage
COPY --from=build-stage /usr/src/app /usr/share/nginx/html
COPY ./default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD nginx -g 'daemon off;'

The docker-compose.yml file is as follows:

services:
    nginx:
      container_name: Nginx
      build:
        context: .
        dockerfile: Dockerfile
      restart: always
      ports:
        - '80:80'
      volumes:
        - ./default.conf:/etc/nginx/conf.d/default.conf
        - ./.:/usr/share/nginx/html
      depends_on:
        - nodejs
      links:
        - nodejs

    nodejs:
      container_name: NodeJS
      build:
       context: .
       dockerfile: Dockerfile
      volumes:
       - ./.:/usr/src/app
      ports:
        - 3000

The default.conf file is as follows:

upstream nodejs {
    server nodejs:3000;
}

server {
   listen 80;
   server_name default_server;
   error_log  /var/log/nginx/error.system-default.log;
   access_log /var/log/nginx/access.system-default.log;
   charset utf-8;


    root /usr/share/nginx/html;
    index index.html index.php index.js;
location ~ \.js$ {
    proxy_pass http://nodejs;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }

    location / {
     autoindex on;
     try_files $uri $uri/ $uri.html =404;
    }
}

I want to run the app.js file and it exists on both containers:

# cd /usr/share/nginx/html
# ls
CONTRIBUTING.md  LICENSE    app.js  cfiles		jenkinsfile	   package.json  routes  views
Dockerfile	 README.md  bin     docker-compose.yml	package-lock.json  public	 test
#
# cd /usr/src/app
# ls
CONTRIBUTING.md  LICENSE    app.js  cfiles		jenkinsfile	   package.json  routes  views
Dockerfile	 README.md  bin     docker-compose.yml	package-lock.json  public	 test

But:

# curl localhost/app.js
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.25.4</center>
</body>
</html>

What is wrong?

Cheers.

NGINX and NodeJS both need their own Dockerfile. Right now you have both running NGiNX. It first installs NodeJS and your app but then throws that out and installs NGiNX instead, for both services.

1 Like

Hi,
Thank you so much for your reply.
You right.
My .gitlab-ci.yml is something like:

image: docker:latest
services:
  - docker:dind
stages:
  - build
  - deploy
build:
  stage: build
  script:
    - docker compose docker-compose.yml build
deploy:
    stage: deploy
    script:
        - docker compose docker-compose.yml up -d

How can I fix it? Should I create two Dockerfile(s) files?

So, yes.

1 Like

3 posts were split to a new topic: Problem with NodeJS module