Dockerfile For Node.js Frontend

A quick summary of Dockerfile For Node.js Frontend

Dockerfile For Node.js Frontend

This is the base Dockerfile can be used to serve Node.js application. Based on Alpine Linux. Average size is 500MB. It includes nginx for serving static content.

Dockerfile


FROM node:12.9.0-alpine

RUN apk update && apk add nginx
RUN mkdir -p /run/nginx
COPY ./frontend_nginx.conf /etc/nginx/conf.d/default.conf

WORKDIR /app

ADD package*.json yarn.lock /app/
RUN yarn install

COPY . /app
RUN yarn build

RUN ln -s /app/.next/ /app/_next

EXPOSE 80
CMD ["sh", "-c", "nginx; yarn start"]

Nginx config file


upstream nodejs {
  server 127.0.0.1:3001;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /app;

        location / {
    	   proxy_max_temp_file_size 0;
           proxy_buffering off;
           try_files $uri @nodejs;
        }
    location /_next/static {
     root /app;
     add_header Cache-Control "public, max-age=31536000, immutable";
     gzip_static on;
    }

  location @nodejs {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Protocol $scheme;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_max_temp_file_size 0;
      proxy_pass http://nodejs;
      proxy_redirect off;
      proxy_read_timeout 240s;
   }
}