A Dockerfile is a build script, and a sloppy one ships slow images, leaked secrets, and builds that fail on a teammate’s machine. These production-ready dockerfile best practices turn a file that happens to work into one you can trust in a pipeline: small images, repeatable builds, no secrets in layers, and a container that runs as a limited user instead of root.
Most Dockerfile problems share one cause. The file was written to get the app running once, then never reviewed. A base image picked at random, dependencies installed before the code is copied, a package manager cache left inside the image, and a process running as root are all silent until they cost you a slow deploy, a failed security scan, or a leaked token. Each practice below fixes one of those risks, and none of them needs a rewrite of your app.
Production-ready Dockerfile best practices start with the base image
Pin the base image to an exact version, such as node:20.18.1-alpine, instead of node:latest or node:20. A floating tag can pull a new minor version overnight and change your build without a single line of your code changing. Exact pins make builds repeatable, and a lock file for your dependencies does the same job for your packages. When you do want an upgrade, change the tag on purpose, test the build, and commit the change.
Pick the smallest base image that fits the app. Good docker image size optimization starts with the base image you choose, because every layer after it adds weight. Alpine variants are a common pick for Node and Python apps because they cut tens of megabytes from the image. Check that your native dependencies still build on Alpine’s musl libc first, because some compiled packages expect glibc and fail in ways that look like missing libraries. Slim Debian variants are the fallback when Alpine fights back. Our guide to multi-stage Dockerfiles that shrink images shows how a small base image pairs with a build stage to cut the final image further.
Order the layers so rebuilds stay fast
Docker caches each layer and reuses it when the inputs have not changed. Put the steps that change least at the top. Copy the dependency manifests first, install dependencies, then copy the app code last. That way a one-line code change rebuilds only the final layers instead of reinstalling every package.
COPY package.json package-lock.json ./ RUN npm ci --omit=dev COPY . .
Use npm ci for Node, pip install --no-cache-dir for Python, and the equivalent lock-file install for your stack, because installing from a lock file gives every build the same dependency tree. Add a .dockerignore file that excludes node_modules, .git, test files, and local environment files, so none of them enter the build context or the image. A small context also makes the build faster, because Docker sends the context to the daemon on every build.
Use multi-stage builds to keep the final image clean
A single-stage image carries the compiler, the build tools, and the source files into production. A multi-stage build keeps those in a build stage and copies only the finished output into a slim runtime stage. The pattern is short and it pays off on every pull:
FROM node:20-alpine AS build WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build FROM node:20-alpine WORKDIR /app COPY package*.json ./ RUN npm ci --omit=dev COPY --from=build /app/dist ./dist USER node CMD ["node", "dist/server.js"]
The runtime stage has no build tools and no source maps, which shrinks the image and the attack surface together. This multi-stage dockerfile build pattern works for Python and Go apps too, with the same split between a build stage and a slim runtime stage. When the image is ready to ship, push it to a registry, and our comparison of the best container registries, Docker Hub vs GHCR vs ECR covers where to store it.
Never run as root and never bake in secrets
Containers run as root by default, which means a bug in your app can write anywhere in the container filesystem and a breakout has more room to hurt the host. Add a USER instruction before CMD and run the app as a limited user. Most official images ship a non-root user already, such as node in the Node image, so the fix is often one line plus a check that the app can still write to the folders it needs.
Secrets deserve the same care. An ENV line with a token becomes part of the image history, and anyone with the image can read it. These dockerfile security best practices apply to every stack, not just Node, because image history works the same way everywhere. Pass secrets at run time with environment variables or a secrets manager, and keep them out of the build entirely. If you want the full picture on handling credentials safely, read our guide to how to store API keys and secrets safely. Build arguments are no better for secrets: they show up in image history too, so use them only for non-secret values such as a version number.
Add a health check and keep the image scannable
A HEALTHCHECK instruction tells Docker how to tell a running container from a working app. A process can be alive while the app is stuck, and without a health check your orchestrator keeps sending traffic to it. Keep the check simple, hitting an endpoint your app already serves:
HEALTHCHECK --interval=30s --timeout=3s CMD wget -qO- http://localhost:3000/health || exit 1
Scan the image before you ship it. Tools such as Trivy and Docker Scout list known vulnerabilities in the base image and your packages, and a pipeline that fails on critical findings stops a bad image before it reaches production. Our guide to hardening a CI/CD pipeline shows where the scan fits in the build steps. Rebuild images on a schedule too, because a pinned base image only stays safe if you refresh it when patches land.
A short checklist before you merge
Run through this list on every Dockerfile change. Base image pinned to an exact version. Dependencies installed from a lock file. Build context trimmed by .dockerignore. Multi-stage build with a slim runtime stage. App runs as a non-root user. No secrets in ENV, ARG, or copied files. Health check present. Image scanned and the scan is clean. If all eight pass, the file is ready for production, and the next developer who touches it can see why each line is there. A Dockerfile earns trust the same way code does: small, explicit steps that anyone can read and repeat.