Guide

Dockerfile best practices: small, fast, safe images

Reviewed 23 November 2026 · 7 min read · Written and maintained by Toolnexo

A Dockerfile is a recipe for a container image, and the same app can be built from a good recipe or a poor one. The poor one is slow to rebuild, ships compilers and secrets it does not need, and runs as the administrator. A few habits fix all of that. This guide explains them with the files made by the Dockerfile generator on this site, which builds the text in your browser, so nothing you type is uploaded. It does not run Docker, so build the image yourself to confirm it works.

What a Dockerfile does

Docker reads the file from the top. FROM picks the base image, WORKDIR sets the folder, COPY brings in files, RUN runs a command at build time, and CMD says what to run when the container starts. Each instruction that changes files adds a layer, and Docker keeps layers so it can reuse them. You build the image with docker build -t my-app . and start it with docker run -p 3000:3000 my-app, where the first 3000 is the port on your computer and the second is the port in the container.

Order matters for speed

Docker reuses a layer only if nothing before it has changed. A Node.js file therefore copies the dependency list first and the code last: COPY package*.json ./, then RUN npm ci --omit=dev, then COPY . . . When you change only your source, the install layer is reused and the rebuild takes seconds. If the code were copied first, every edit would reinstall every dependency. The same idea applies to requirements.txt for Python and go.mod for Go.

Choosing a base image

  • Pin a version, such as node:22-alpine, instead of latest, so a build next month uses the same base as today.
  • Alpine images are several times smaller than the full ones, but they use a different C library, which can make some native packages slow to build or fail. That is why the Python template uses python:3.12-slim.
  • Use a runtime image to run and a bigger image to build. For Java, eclipse-temurin:21-jre runs the app, and a Maven image builds it.
  • Tags change over time, so confirm on Docker Hub that the tag you pick exists.

Multi-stage builds

A multi-stage Dockerfile has more than one FROM. Early stages install tools and build the app, and the last stage copies in only the result. For Node.js with a build step, the first stage runs npm ci and npm run build, and the final stage runs npm ci --omit=dev, copies the dist folder with COPY --from=build /app/dist ./dist, and starts the app. The final image has the production dependencies and the build output but not the dev tools.

Go shows the gain best. The first stage uses golang:1.24-alpine and runs CGO_ENABLED=0 go build -o /out/app . The second stage starts from alpine:3.20 and copies only the program. The compiler and source never reach the image you ship.

Do not run as root

Containers run as root by default. If someone breaks into the app, root inside the container gives them far more to work with. Node.js images already include a user called node, so one line, USER node, is enough. For Python the generator adds RUN useradd --create-home app and USER app. For Alpine it uses adduser -D app. Most apps run fine this way. The tick box can be turned off if yours needs root, but think about why first.

Write CMD in exec form

CMD ["node", "server.js"], with the command split into a list, runs your app directly. The shell form, CMD node server.js, runs it through a shell, so the app may not receive the stop signal when you run docker stop, and Docker waits ten seconds before killing it. The generator splits your start command into words, respects quotes, and writes the list for you, so node dist/index.js --name "a b" becomes four items with the last being a b. A quote that is never closed is reported instead of written out wrongly.

Keep the context and the secrets clean

Docker sends the build folder to the builder before it starts. A .dockerignore file leaves out what the image does not need, such as node_modules, .git, logs and .env files. This makes builds faster and keeps local clutter out. Never bake passwords or keys into an image. Anyone who gets the image can read every layer. Pass secrets when the container starts, with an environment variable or a secret store.

Health checks

A HEALTHCHECK line lets Docker test whether the app still answers. The generator adds one if you give a path such as /health, with a check every 30 seconds, a 3-second timeout and a 10-second start period. It uses what the image already has: the built-in fetch for Node.js, urllib for Python and wget for Go and nginx, so no extra packages are installed. Your app needs to answer on that path for the check to pass.

Using the generator

  • Pick Node.js, Python, Go, Java or a static site, then the version, the port and the start command. For Node.js you can add a build command and choose npm, Yarn or pnpm.
  • File names are assumed: package.json with a lock file, requirements.txt, go.mod and pom.xml. Edit the result if yours differ.
  • The Java file builds with Maven and expects one jar in target. Gradle projects need a different file.
  • Copy or download the Dockerfile, copy the .dockerignore, and run the two commands shown.
Tools in this guide

Put it into practice

How this guide is maintained

This page is reviewed when the related tools change. It describes what the published tools actually do: the tools run in your browser tab, so your files stay on your device. If you find something inaccurate, email [email protected] and it will be corrected.

Keep reading

Other guides

All guides →