Quick reference for writing production-friendly Dockerfiles.
Core instructions
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node","server.js"]
Layer cache strategy
Copy dependency manifests first, install, then copy app code. This keeps dependency layers reusable.
Multi-stage build
FROM node:20 AS build
WORKDIR /app
COPY . .
RUN npm ci && npm run build
FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/html
Must-have .dockerignore
node_modules
.git
.env
coverage
dist
Security tips
- Prefer slim/alpine base images.
- Do not copy secrets into image layers.
- Use non-root user when possible.