Skip to content

Instantly share code, notes, and snippets.

@hunghg255
Last active November 19, 2024 01:59
Show Gist options
  • Save hunghg255/f25f616d2b8c71fd846c1472af9108b6 to your computer and use it in GitHub Desktop.
Save hunghg255/f25f616d2b8c71fd846c1472af9108b6 to your computer and use it in GitHub Desktop.
React Static Docker Nginx
FROM node:18-alpine AS base
# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json ./
COPY package-lock.json ./
RUN npm install
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN npm run buildProd
EXPOSE 80
#Stage 2
FROM nginx:1.19.0
COPY --from=builder /app/nginx.conf /etc/nginx/conf.d/default.conf
WORKDIR /usr/share/nginx/html
RUN rm -rf ./*
COPY --from=builder /app/dist .
ENTRYPOINT ["nginx", "-g", "daemon off;"]
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html;
try_files $uri /index.html; # Enable client-side routing
}
error_page 404 /index.html; # Handle 404 errors with React app
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment