Shipping full Node.js runtimes, package managers, and node_modules into production containers inflates deployment times, exhausts bandwidth, and increases your CVE security attack surface. By implementing a two-stage Docker build paired with Alpine Nginx, you can shrink production bundle images down to under 25 MB.
Production-Ready Multi-Stage Dockerfile
# Stage 1: Build & compilation
FROM node:lts-alpine AS builder
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
COPY . .
RUN yarn build
# Stage 2: Minimalist production web server
FROM nginx:alpine-slim
COPY --from=builder /app/build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Key Production Advantages
- Zero Node Runtime in Production: The final image contains only static web assets and a hardened Nginx binary.
- Layer Cache Optimization: Copying
package.jsonandyarn.lockseparately prevents cache busting on unchanged dependency trees. - Security Hardening: Minimal OS packages significantly decrease vulnerability scan alerts from Trivy, Snyk, and AWS ECR.