Docker rewards a little discipline. These are the practices that keep it maintainable as a team and a codebase grow.
The practices
- Pin base image tags (or digests) instead of :latest for reproducibility
- Order layers from least- to most-frequently changed to maximize cache hits
- Use multi-stage builds to keep runtime images tiny and attack surface small
- Add a HEALTHCHECK and run as a non-root user
What good looks like
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"]Anti-patterns to retire
- Running as root inside the container - always add a non-root USER
- Fat images from a single-stage build; use multi-stage and slim/alpine bases
- Baking secrets into layers with COPY or ENV - they stay in the image history
- No .dockerignore, so node_modules and .git bloat the build context
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.