Most Docker pain is self-inflicted and predictable. Here are the mistakes we see most, and the fix for each.
Mistake 1: Running as root inside the container - always add a non-root USER
Fix: Pin base image tags (or digests) instead of :latest for reproducibility. Make it the default in your templates and enforce it in review or CI so the mistake cannot come back.
Mistake 2: Fat images from a single-stage build; use multi-stage and slim/alpine bases
Fix: Order layers from least- to most-frequently changed to maximize cache hits. Make it the default in your templates and enforce it in review or CI so the mistake cannot come back.
Mistake 3: Baking secrets into layers with COPY or ENV - they stay in the image history
Fix: Use multi-stage builds to keep runtime images tiny and attack surface small. Make it the default in your templates and enforce it in review or CI so the mistake cannot come back.
Mistake 4: No .dockerignore, so node_modules and .git bloat the build context
Fix: Add a HEALTHCHECK and run as a non-root user. Make it the default in your templates and enforce it in review or CI so the mistake cannot come back.
A safer baseline
A small, cache-friendly multi-stage Dockerfile for a Node service.
# syntax=docker/dockerfile:1
FROM node:20-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:20-alpine AS run
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app/dist ./dist
COPY --from=build /app/node_modules ./node_modules
USER node
EXPOSE 3000
CMD ["node", "dist/server.js"]Want senior help putting Docker to work - or a team bootcamp on it? CodeWizard runs Israel's largest DevOps community and embeds named senior engineers with a transparent hours log. Book a free consult or explore the Academy.