Developer and Text Tools

Dockerfile Best Practices: Smaller, Faster and Safer Images

Common Dockerfile mistakes that bloat images, slow builds and weaken security, and how to lint and rewrite a Dockerfile to fix them.

Saizul Amin
Saizul Amin3 Nov 2026 · 3 min read
Dockerfile Best Practices: Smaller, Faster and Safer Images

Most Dockerfiles build. The question is whether they build small, fast, repeatable images that do not run as root or carry secrets around. A handful of patterns decide that, and they are easy to check automatically. This guide covers the ones that matter most and shows how the Dockerfile Linter and Optimizer finds and fixes them.

Pin the base image

FROM node or FROM node:latest pulls whatever was published most recently. Next month the same Dockerfile can build on a different major version and break. Pin a version such as node:22-alpine, or a digest for exact reproducibility, and update it deliberately.

Order layers for the cache

Docker caches each instruction's result and reuses it until something above it changes. If the Dockerfile copies the whole project and then installs dependencies, any code change invalidates the install layer and every build reinstalls everything. Copy only the dependency manifests first, install, then copy the rest. The linter flags installs that come after COPY . . or ADD . . for this reason.

Clean up in the same layer

A file deleted in a later layer still takes space in the earlier one. Package manager caches must be removed in the same RUN that created them. For Debian and Ubuntu images that means apt-get update and install together, with -y and --no-install-recommends, followed by removing /var/lib/apt/lists. For Alpine, apk add --no-cache. For Python, pip install --no-cache-dir. For Node, npm ci installs exactly what the lock file says and is faster than npm install in builds.

Do not run as root

Containers run as root unless told otherwise. If the application is compromised, root inside the container has much more power over mounted volumes and the kernel interface. Create or use a non-root user and switch to it with USER before the start command. Many official images already include one, such as node.

Keep secrets out of the image

ENV and ARG values are stored in the image metadata and history, so anyone who can pull the image can read them. The linter treats secret-looking names in ENV and ARG as errors. Use BuildKit secret mounts for build-time credentials and inject runtime secrets from your orchestrator.

Smaller details that add up

  • Use COPY for local files; ADD also unpacks archives and fetches URLs, which surprises people.
  • Write CMD and ENTRYPOINT in exec form so the app receives stop signals directly.
  • Use absolute WORKDIR paths instead of cd inside RUN.
  • Add a HEALTHCHECK so Docker and Compose know when the app is ready.
  • Avoid curl piped into a shell, sudo and chmod 777.
  • Use multi-stage builds so compilers and source files stay out of the final image.

Using the linter

Paste a Dockerfile and you get a score out of 100 and a list of findings with line numbers, rule codes and fixes. Errors cost 15 points, warnings 6 and notices 2. Filter by level to work through the serious ones first.

The rewrite tab applies the safe fixes automatically: MAINTAINER becomes a LABEL, local ADD becomes COPY, package manager flags and cleanup are added, npm install can become npm ci, CMD switches to exec form, consecutive RUN lines can be merged and a USER can be added before the start command. The rewritten file is linted again so you can see the improvement. Choices that need judgement, such as which base tag to pin or how to restructure layers, are left to you.

The templates tab includes a Node multi-stage example and a starter .dockerignore that keeps .git, node_modules and env files out of the build context.

Limits

The checks read text. They do not run a build, pull images or scan for vulnerable packages, so combine them with an image scanner in your pipeline.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages