From 2af996b83e4c6802b0626628241976e504b84be9 Mon Sep 17 00:00:00 2001 From: Arnab-Afk Date: Wed, 23 Jul 2025 19:31:13 +0530 Subject: [PATCH] Add Dockerfile and Nginx configuration for multi-stage build setup --- Dockerfile | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ nginx.conf | 29 +++++++++++++++++++++++++ nginx/nginx.conf | 26 ++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 Dockerfile create mode 100644 nginx.conf create mode 100644 nginx/nginx.conf diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fe5494b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,56 @@ +# Stage 1: Build the React frontend +FROM node:18-alpine AS frontend-builder +WORKDIR /app/frontend + +# Define a build-time argument for the API URL with a default value +ARG VITE_API_URL="" +# Set it as an environment variable for the build command +ENV VITE_API_URL=$VITE_API_URL + +# Copy package files and install dependencies +COPY Frontend/package.json Frontend/package-lock.json* Frontend/yarn.lock* ./ +RUN yarn install --frozen-lockfile + +# Copy the rest of the frontend source code +COPY Frontend/ ./ + +# Build the static files. Vite will use the VITE_API_URL env var. +RUN yarn build + +# Stage 2: Build the Go backend +FROM golang:1.19-alpine AS backend-builder +WORKDIR /app/backend + +# Install git for dependency fetching +RUN apk update && apk add --no-cache git + +# Copy go module files and download dependencies +COPY new-backend/go.mod new-backend/go.sum ./ +RUN go mod download + +# Copy the backend source code +COPY new-backend/ ./ + +# Build the Go binary +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags="-s -w" -o /monaco-backend . + +# Stage 3: Create the final image with Nginx +FROM nginx:1.25-alpine + +# Install Docker client for the backend +RUN apk update && apk add --no-cache docker-cli + +# Copy the Go backend binary +COPY --from=backend-builder /monaco-backend /usr/local/bin/monaco-backend + +# Copy the built frontend files to the Nginx html directory +COPY --from=frontend-builder /app/frontend/dist /usr/share/nginx/html + +# Copy the Nginx configuration +COPY nginx.conf /etc/nginx/nginx.conf + +# Expose the public port for Nginx +EXPOSE 80 + +# Start both the backend and Nginx +CMD sh -c 'monaco-backend & nginx -g "daemon off;"' \ No newline at end of file diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..e30e44b --- /dev/null +++ b/nginx.conf @@ -0,0 +1,29 @@ +events {} + +http { + include /etc/nginx/mime.types; # <-- ADD THIS LINE + + server { + listen 80; + server_name localhost; + + # Serve the static frontend files + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri $uri/ /index.html; + } + + # Proxy API requests to the Go backend + location /api/ { + proxy_pass http://localhost:8081; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + } +} \ No newline at end of file diff --git a/nginx/nginx.conf b/nginx/nginx.conf new file mode 100644 index 0000000..b0a7d44 --- /dev/null +++ b/nginx/nginx.conf @@ -0,0 +1,26 @@ +events {} + +http { + include /etc/nginx/mime.types; # <-- ADD THIS LINE + + server { + listen 80; + server_name localhost; + + # Serve the static frontend files + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri $uri/ /index.html; + } + + # Proxy API requests to the Go backend + location /api/ { + proxy_pass http://localhost:8080; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + } +} \ No newline at end of file