Smaller, Safer Images
A checklist for production images: minimal base, nothing secret, nothing extra, no root, pinned versions, and a habit of checking what you ship.
Why size and safety go together
Every file in an image is something to download on each deploy, something that slows a cold start, and something that might contain a vulnerability. Smaller images are faster and safer, because there is less in them to attack.
1. Start from a minimal base
node:22-slim instead of node:22. The slim variant leaves out compilers, documentation and tools a running web server never touches. Going further, distroless images contain only the runtime and no shell at all. They are even smaller and harder to attack, but also harder to debug, because you cannot open a shell inside. For a small site, slim is the sensible middle.
2. Install only production dependencies
npm ci --omit=dev keeps test frameworks, linters and build tools out of the image. On many Node projects, dev dependencies are larger than the app.
3. Let .dockerignore do the heavy lifting
A good ignore file keeps out: .git (often the biggest folder in the repository), .env and any local settings, node_modules (installed fresh inside), backups, archives, reports, scratch scripts and docs. Review it whenever a new kind of file appears in the repository.
4. Never bake in a secret
No ENV API_KEY=... lines, no .env copied in, no credentials passed as build arguments (they are recorded in the image history). Secrets are supplied when the container starts, by the platform. An image should be safe to publish even if, by accident, it is.
5. Do not run as root
USER node (or any unprivileged user) before CMD. Keep files owned by that user with COPY --chown, so the app does not need write access it should not have.
6. Pin what you depend on
- The base image by major version (
node:22-slim). For maximum reproducibility you can pin by digest (node:22-slim@sha256:...), at the cost of updating it by hand. - Packages by lock file, installed with
npm ci.
Pinning means a rebuild gives the same result. The other half of the bargain is to update on purpose, regularly, rather than never.
7. Clean up in the same layer
Anything downloaded and deleted in a later RUN still sits in the earlier layer. Download, use and remove in one RUN instruction.
8. One process per container
The container runs the web server and nothing else: no cron daemon, no SSH server. Scheduled jobs on this site are separate: a cloud scheduler calls a protected address on the site, and a GitHub workflow runs speed checks. Each part can fail, scale and be replaced on its own.
9. Scan and patch
Base images and packages get security fixes all the time. Habits that keep up with them:
- rebuild regularly, so the base image picks up patched system packages;
- run
npm audit --omit=devand fix what affects production packages; - if your registry offers vulnerability scanning (Artifact Registry does), switch it on and read the findings for the image you actually deploy.
The checklist
| Check | How to verify |
|---|---|
| Slim, pinned base | First line of the Dockerfile |
| Production dependencies only | npm ci --omit=dev in the Dockerfile |
| No secrets inside | Open a shell in the image and look for .env |
| Not root | whoami inside the container |
| Nothing extra | docker history for unexpected large layers |
| Up to date | Recent rebuild; clean npm audit --omit=dev |
Try it yourself
Run the checklist against the image you deploy today. Turn the two most important rows into automated checks: this site has a test that fails if the Dockerfile ever loses its USER line.