From 748fd63cf240bbdd8ef753aa14012409dd2e77e7 Mon Sep 17 00:00:00 2001 From: arnab2001 Date: Thu, 8 Feb 2024 01:07:14 +0530 Subject: [PATCH] nginx docker prod --- client/.dockerignore | 3 +++ client/Dockerfile | 40 ++++++++++++++++++++++++++++++---------- client/Dockerfile.prod | 29 +++++++++++++++++++++++++++++ client/nginx.conf | 29 +++++++++++++++++++++++++++++ server/.gitignore | 1 + 5 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 client/.dockerignore create mode 100644 client/Dockerfile.prod create mode 100644 client/nginx.conf diff --git a/client/.dockerignore b/client/.dockerignore new file mode 100644 index 0000000..bf8a8e0 --- /dev/null +++ b/client/.dockerignore @@ -0,0 +1,3 @@ +node_modules +build +npm-debug.log \ No newline at end of file diff --git a/client/Dockerfile b/client/Dockerfile index 21751f7..4a10493 100644 --- a/client/Dockerfile +++ b/client/Dockerfile @@ -1,21 +1,41 @@ -# Build react client -FROM node:16-alpine +FROM node:lts-alpine as build -# Working directory be app -WORKDIR /usr/src/app +WORKDIR /app COPY package*.json ./ -### Installing dependencies +RUN npm ci -RUN npm install --silent - -# copy local files to app folder COPY . . -EXPOSE 3000 +RUN npm run build +FROM nginx:latest as prod + +COPY --from=build /app/build /usr/share/nginx/html +COPY nginx.conf /etc/nginx/nginx.conf + +EXPOSE 80/tcp + +CMD ["/usr/sbin/nginx", "-g", "daemon off;"] + +# # Build react client +# FROM node:16-alpine + +# # Working directory be app +# WORKDIR /usr/src/app + +# COPY package*.json ./ + +# ### Installing dependencies + +# RUN npm install --silent + +# # copy local files to app folder +# COPY . . + +# EXPOSE 3000 -CMD ["npm","start"] +# CMD ["npm","start"] # docker build -t arnabchat2001/client . # docker tag arnabchat2001/client arnabchat2001/client:v2 diff --git a/client/Dockerfile.prod b/client/Dockerfile.prod new file mode 100644 index 0000000..f22dba4 --- /dev/null +++ b/client/Dockerfile.prod @@ -0,0 +1,29 @@ +# Use the official Node.js runtime as the base image +FROM node:18 as build + +# Set the working directory in the container +WORKDIR /app + +# Copy package.json and package-lock.json to the working directory +COPY package*.json ./ + +# Install dependencies +RUN npm install + +# Copy the entire application code to the container +COPY . . + +# Build the React app for production +RUN npm run build + +# Use Nginx as the production server +FROM nginx:alpine + +# Copy the built React app to Nginx's web server directory +COPY --from=build /app/build /usr/share/nginx/html + +# Expose port 80 for the Nginx server +EXPOSE 80 + +# Start Nginx when the container runs +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/client/nginx.conf b/client/nginx.conf new file mode 100644 index 0000000..11a54dc --- /dev/null +++ b/client/nginx.conf @@ -0,0 +1,29 @@ +http { + + include mime.types; + + set_real_ip_from 0.0.0.0/0; + real_ip_recursive on; + real_ip_header X-Forward-For; + limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s; + + server { + listen 80; + server_name localhost; + root /proxy; + limit_req zone=mylimit burst=70 nodelay; + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri /index.html; + } + + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + } +} + +events {} \ No newline at end of file diff --git a/server/.gitignore b/server/.gitignore index e50d5f6..0e5941d 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -15,6 +15,7 @@ package-lock.json /server/public # misc .DS_Store + .env .env.local .env.development.local