Docker for Production: Beyond the Basics
Every developer knows how to write a Dockerfile. Few write Dockerfiles that are production-ready. The gap between a working container and a production container is security, performance and operational maturity.
These are the practices that close it. The examples use Node.js, but the ideas apply to any stack.
Start with multi-stage builds
Multi-stage builds are the foundation. Your build stage includes compilers, dev dependencies and build tools. Your runtime stage includes only what's needed to run.
For a Node.js app, the build stage runs npm ci and npm run build. The runtime stage copies only the production node_modules and the build output. This typically reduces image size by 60-80%.
FROM node:24.22.1-slim AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build && npm prune --omit=dev
FROM node:24.22.1-slim AS runtime
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
USER node
CMD ["node", "dist/main.js"]
A smaller image pulls faster, starts faster, and carries fewer packages that could contain a vulnerability.
Choose your base image deliberately
Base image selection matters more than people think. Alpine images are smaller but use musl instead of glibc, which causes subtle compatibility issues with some Node.js native modules.
I use a Debian-based slim image, such as node:24-slim, for production. I use the Alpine variant only when I've verified all dependencies work correctly. Saving a few megabytes is not worth a native module that fails in a way you only discover under load.
Run as a non-root user. Always.
Create a dedicated user in your Dockerfile and switch to it before the CMD instruction. The official Node images already include a node user, so one line is enough:
USER node
This limits the blast radius if your application is compromised. Cloud Run and most Kubernetes configurations enforce this, but your Dockerfile should be explicit about it. Don't rely on the platform to cover for an image that would happily run as root elsewhere.
Order layers for the cache
Layer ordering is a performance optimization that pays off daily. Docker caches layers, and a change to one layer invalidates every layer after it.
So put instructions that change infrequently before instructions that change often:
- Install system packages.
- Copy
package.jsonand the lockfile, then install dependencies. - Copy the source code.
With that order, editing a source file reuses the cached dependency layer. A well-ordered Dockerfile rebuilds in seconds instead of minutes.
Put a health check in the image
Health checks belong in the Dockerfile, not just the orchestrator configuration. A HEALTHCHECK instruction tells Docker how to verify your container is actually serving traffic, not just running.
HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
CMD node -e "fetch('http://localhost:8080/health').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"
For web services, a simple HTTP check against a /health endpoint is sufficient. A process can be alive and still unable to answer a request, and this is how you tell the difference.
Keep secrets out of the build
Environment variables should not contain secrets at build time. Anything passed during a build can end up stored in the image's layers and history.
- Build-time configuration, such as
NODE_ENVor API URLs, usesARG. - Runtime configuration uses
ENV. - Secrets, such as database passwords and API keys, should be injected at runtime by your orchestrator.
Use multi-stage builds to keep the two apart, so nothing from the build stage's environment leaks into the image you ship.
Don't forget .dockerignore
One often-missed optimization: .dockerignore. Without it, Docker sends your entire project directory, including node_modules, .git and test fixtures, to the build context.
node_modules
.git
.env*
coverage
dist
*.log
A proper .dockerignore reduces build context transfer from gigabytes to megabytes. It also keeps local .env files from being copied into an image by accident.
Pin your base image versions
Finally, pin your base image versions. FROM node:24 will silently change when a new patch version is released. FROM node:24.22.1-slim is reproducible.
I accept minor version updates but pin patch versions, updating them intentionally during maintenance windows. The exception is security releases. When one lands, bumping the pin is the maintenance window.
Production Dockerfile checklist
- Multi-stage build, with only runtime files in the final stage.
- A slim, glibc-based base image unless Alpine is verified.
- A non-root user set before
CMD. - Dependency installation before source code, for caching.
- A
HEALTHCHECKagainst a real endpoint. - No secrets in
ARG,ENVor copied files. - A
.dockerignorefile. - A pinned base image version.
To see these applied to a real framework, read Deploying Next.js to Google Cloud Run. To build and ship the image automatically, see CI/CD with GitHub Actions: Patterns That Work.