A Docker image that contains the compiler, the dev dependencies and the source code is larger and less safe than it needs to be. Multi-stage builds solve this: one stage builds the app, a second stage contains only what runs it.

A static frontend: build with Node, serve with Nginx

An Angular or React app compiles to static files. Node is needed to build them and not at all to serve them.

# Stage 1: build
FROM node:22-alpine AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build

# Stage 2: serve
FROM nginx:1.27-alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/dist/my-app/browser /usr/share/nginx/html
EXPOSE 80

The final image is Nginx plus the built files, a few tens of megabytes. node_modules and the source never leave the first stage. Adjust the copied path to your build output: dist/<name>/browser for Angular, dist for Vite, out for a Next.js static export.

For client-side routing, Nginx must return index.html for unknown paths:

server {
  listen 80;
  root /usr/share/nginx/html;

  location / {
    try_files $uri $uri/ /index.html;
  }

  location ~* \.(js|css|woff2|png|jpg|webp|svg)$ {
    expires 1y;
    add_header Cache-Control "public, immutable";
  }
}

Long caching is safe for those files because build tools put a content hash in their names.

A Node API: drop the dev dependencies

FROM node:22-alpine AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:22-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY --from=build /app/dist ./dist
USER node
EXPOSE 3000
CMD ["node", "dist/main.js"]

The runtime stage installs production dependencies only and copies the compiled output. TypeScript, test tools and linters stay behind.

Order layers so the cache works

Docker caches each instruction and reuses it while its inputs are unchanged. Copying package.json and the lockfile before the rest of the source means npm ci reruns only when dependencies change. Copy everything first and every code edit reinstalls every package.

Keep the build context small

A .dockerignore file stops local clutter from being sent to the build and from invalidating the cache:

node_modules
dist
.git
.env
*.log

Excluding .env also keeps local secrets out of the image by accident.

Small things that matter in production

  • Run as a non-root user. The official Node image ships a node user; one USER node line uses it.
  • Pin versions. node:22-alpine moves with patch releases; pin a digest when you need exact reproducibility.
  • Use npm ci, not npm install. It installs exactly what the lockfile says and fails if they disagree.
  • Pass configuration at run time. Environment variables belong in docker run or the orchestrator, not baked in with ENV at build time. Secrets never go in a Dockerfile.
  • Add a health check so the platform knows when the container is ready.

Check the result with docker images. A frontend image well under 100 MB and an API image a fraction of the size of the single-stage version are normal outcomes, and smaller images pull faster and have less in them to patch.