ololo
ololo

Reputation: 1996

Page isn't working error when starting docker container with nginx

I wish to deploy an Express server + GRPC Server in one node process in Cloud Run.

Based on suggestions I gathered, the way to go about that is with nginx.

So I have my docker file as below:

Dockerfile

# Build environment
FROM node:16.14.2
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=production
COPY . ./

# server environment
FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/configfile.template
ENV HOST 0.0.0.0
ENV NODE_ENV production
EXPOSE 8080

RUN printf '%s\n' >cmd.sh \
    "envsubst '\$PORT' < /etc/nginx/conf.d/configfile.template > /etc/nginx/conf.d/default.conf &&" \
    "nginx -g 'daemon off;' &&" \
    "node server.js"
    
CMD ["sh", "cmd.sh"]

Then my nginx.conf file is as below:

   upstream grpc-server {
       server 0.0.0.0:4000;
   }

   upstream express-server{
       server 0.0.0.0:3000;
   }

   server {

    # listen       $PORT;
    listen         8080;
    server_name    user-service;

    location / {
      root public;
      index index.html;
    }

    location /user {
        grpc_pass  grpc://grpc-server;
    }

    location /v1 {
        proxy_pass http://express-server;
    }
    
  }

However, whenever I start my docker container using docker run -p 8080:80 user-service I keep getting the following logs

2022/11/12 21:25:38 [notice] 9#9: using the "epoll" event method
2022/11/12 21:25:38 [notice] 9#9: nginx/1.23.2
2022/11/12 21:25:38 [notice] 9#9: built by gcc 11.2.1 20220219 (Alpine 11.2.1_git20220219) 
2022/11/12 21:25:38 [notice] 9#9: OS: Linux 5.10.104-linuxkit
2022/11/12 21:25:38 [notice] 9#9: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/11/12 21:25:38 [notice] 9#9: start worker processes
2022/11/12 21:25:38 [notice] 9#9: start worker process 10
2022/11/12 21:25:38 [notice] 9#9: start worker process 11
2022/11/12 21:25:38 [notice] 9#9: start worker process 12
2022/11/12 21:25:38 [notice] 9#9: start worker process 13

Now, when I visit http://localhost:8080 I am constantly getting:

This page isn’t working

localhost didn’t send any data.

ERR_EMPTY_RESPONSE

However, if I remove the Dockerfile above and use my locally installed nginx server and the nginx.conf file above everything works perfectly.

NB: The This Page isn't working error only comes up once I start using Dockerfile.

What could be the issue?

Upvotes: 0

Views: 769

Answers (1)

Sergio Santiago
Sergio Santiago

Reputation: 1514

Your port redirect, when you are running the docker container is pointing to port 80, but it should be port 8080 where nginx is listening to:

docker run -p 8080:8080 user-service

Upvotes: 1

Related Questions