|
1 | 1 | # ------------------------------------------------------- |
2 | | -# 1) Build the React app using Vite |
| 2 | +# Multi-Stage Build: Builder Stage |
| 3 | +# - Reduces final image size |
| 4 | +# - Removes dev dependencies |
| 5 | +# - Improves CI/CD caching |
3 | 6 | # ------------------------------------------------------- |
4 | 7 | FROM node:20-alpine AS builder |
| 8 | + |
| 9 | +# Use WORKDIR early (cached) |
5 | 10 | WORKDIR /app |
6 | 11 |
|
| 12 | +# Layer Caching |
| 13 | +# Copy only package* first so npm install is cached unless deps change |
7 | 14 | COPY package*.json ./ |
8 | | -RUN npm install |
9 | 15 |
|
| 16 | +# Install dependencies (use ci for reproducibility) |
| 17 | +RUN npm ci --include=dev |
| 18 | + |
| 19 | +# Copy the rest of the source code (last layer → faster caching) |
10 | 20 | COPY . . |
| 21 | + |
| 22 | +# Build the React/Vite app |
11 | 23 | RUN npm run build |
12 | 24 |
|
13 | 25 |
|
| 26 | + |
14 | 27 | # ------------------------------------------------------- |
15 | | -# 2) Nginx stage (serves the compiled app) |
| 28 | +# Final Stage — Optimized Nginx Server |
| 29 | +# - Serves static files efficiently |
| 30 | +# - Small attack surface |
| 31 | +# - Production-grade caching |
16 | 32 | # ------------------------------------------------------- |
17 | 33 | FROM nginx:1.27-alpine |
18 | 34 |
|
19 | 35 | # Remove default config |
20 | 36 | RUN rm /etc/nginx/conf.d/default.conf |
21 | 37 |
|
22 | | -# Copy your optimized Nginx config |
| 38 | +# Copy your optimized Nginx configuration |
23 | 39 | COPY nginx-config/app.conf /etc/nginx/conf.d/default.conf |
24 | 40 |
|
25 | | -# Copy build output |
| 41 | +# Copy only the optimized production build |
26 | 42 | COPY --from=builder /app/dist /usr/share/nginx/html |
27 | 43 |
|
28 | | -# Expose port 80 |
| 44 | +# ------------------------------------------------------- |
| 45 | +# Expose Ports Explicitly |
| 46 | +# Helps readability & k8s ingress controllers |
| 47 | +# ------------------------------------------------------- |
29 | 48 | EXPOSE 80 |
30 | 49 |
|
| 50 | +# ------------------------------------------------------- |
| 51 | +# Add Healthcheck (for Docker + Kubernetes) |
| 52 | +# Ensures container restart on failure |
| 53 | +# ------------------------------------------------------- |
| 54 | +HEALTHCHECK --interval=30s --timeout=5s --start-period=10s \ |
| 55 | + CMD wget -qO- http://localhost || exit 1 |
| 56 | + |
| 57 | +# ------------------------------------------------------- |
| 58 | +# Metadata (optional but good practice) |
| 59 | +# ------------------------------------------------------- |
| 60 | +LABEL maintainer="Firas Mosbahi" |
| 61 | +LABEL description="Optimized production-ready Docker image for a Vite/React application served via Nginx." |
| 62 | + |
| 63 | +# Run NGINX in foreground |
31 | 64 | CMD ["nginx", "-g", "daemon off;"] |
0 commit comments