MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Dockerizing React Applications with Multi-Stage Builds0%
Study Materials
React.js for AdvancedProgress 0%
1.1Control Props and Inversion of Control Pattern1.2State Reducer Pattern in Advanced React1.3Prop Getters and Flexible Design Systems1.4Headless UI Architecture and Accessibility Primitives
17.1Production Build Optimization and Asset Compression17.2Dockerizing React Applications with Multi-Stage Builds17.3Automated CI/CD Pipelines with GitHub Actions17.4Edge CDN Deployment with Cloudflare, Vercel, and Netlify
HomeTutorialsReact.js for AdvancedDockerizing React Applications with Multi-Stage Builds
Automated CI/CD Pipelines with GitHub Actions

Dockerizing React Applications with Multi-Stage Builds

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

Containerization guarantees that a React application builds and runs identically across local development, staging environments, and production Kubernetes clusters. A naive Docker build copies all source code and node_modules into the final image, resulting in massive container images (>1.5 GB) riddled with security vulnerabilities. Multi-Stage Docker Builds isolate the Node.js build tools and compile static HTML/JS/CSS assets into a lightweight, ultra-secure Nginx container (<25 MB).


1. Multi-Stage Build Architecture

1Stage 1: Build Environment (Node.js Alpine) - ~450 MB
2├── Installs npm dependencies
3├── Compiles TypeScript
4├── Runs vite build
5└── Generates /app/dist static folder
6 │
7 │ (Copies ONLY the compiled /dist folder!)
8 ▼
9Stage 2: Production Runtime (Nginx Alpine) - ~25 MB
10├── Lightweight high-performance web server
11├── No Node.js runtime, no npm, no source code!
12└── Serves pre-compressed static assets at 10,000 req/sec

2. Production Dockerfile

1# ==========================================
2# STAGE 1: Build Environment
3# ==========================================
4FROM node:20-alpine AS builder
5 
6WORKDIR /app
7 
8# Install dependencies first (leverages Docker layer caching)
9COPY package.json package-lock.json ./
10RUN npm ci --prefer-offline --no-audit
11 
12# Copy source files and compile
13COPY . .
14RUN npm run build
15 
16# ==========================================
17# STAGE 2: Production Nginx Server
18# ==========================================
19FROM nginx:1.25-alpine AS runner
20 
21# Remove default nginx static html
22RUN rm -rf /usr/share/nginx/html/*
23 
24# Copy compiled static assets from builder stage
25COPY --from=builder /app/dist /usr/share/nginx/html
26 
27# Copy custom enterprise Nginx configuration
28COPY nginx.conf /etc/nginx/conf.d/default.conf
29 
30# Expose standard HTTP port
31EXPOSE 80
32 
33# Run nginx in foreground
34CMD ["nginx", "-g", "daemon off;"]

3. Production Nginx Configuration (nginx.conf)

Single Page Applications require routing all non-file URL requests back to index.html to allow React Router to handle client-side routing. Without try_files $uri $uri/ /index.html, refreshing /dashboard returns a 404 Not Found!

1server {
2 listen 80;
3 server_name localhost;
4 
5 root /usr/share/nginx/html;
6 index index.html;
7 
8 # Enable Gzip compression
9 gzip on;
10 gzip_vary on;
11 gzip_proxied any;
12 gzip_comp_level 6;
13 gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;
14 
15 # SPA Fallback: Direct all unknown paths to index.html for client-side routing
16 location / {
17 try_files $uri $uri/ /index.html;
18 }
19 
20 # Long-term immutable caching for hashed static assets
21 location /assets/ {
22 expires 1y;
23 add_header Cache-Control "public, max-age=31536000, immutable";
24 access_log off;
25 }
26 
27 # Security Headers
28 add_header X-Frame-Options "SAMEORIGIN" always;
29 add_header X-Content-Type-Options "nosniff" always;
30 add_header X-XSS-Protection "1; mode=block" always;
31 add_header Referrer-Policy "strict-origin-when-cross-origin" always;
32}

4. Dockerignore Configuration (.dockerignore)

Prevent copying local build artifacts and sensitive files into the Docker context:

1node_modules
2dist
3.git
4.env
5.env.local
6npm-debug.log
7.DS_Store

5. Building and Running the Container

1# Build optimized image tagged as enterprise-react:latest
2docker build -t enterprise-react:latest .
3 
4# Run container on port 8080
5docker run -d -p 8080:80 --name react-app enterprise-react:latest

Inspect the final image with docker images: notice the image size is under 25 MB, with zero Node.js vulnerabilities!


Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is the primary advantage of Multi-Stage Docker builds for React applications?
Question 2
Why is try_files $uri $uri/ /index.html mandatory in an Nginx SPA configuration?
Question 3
Why is COPY package*.json ./ followed by RUN npm ci placed before COPY . . in the Dockerfile?
Question 4
Why is npm ci preferred over npm install in Docker builds?
Question 5
What security benefit is achieved by discarding the Node.js runtime from the final production container?
Next Lesson

Automated CI/CD Pipelines with GitHub Actions

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Production Build Optimization and Asset CompressionAutomated CI/CD Pipelines with GitHub Actions

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master React.js for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext