Most Dockerfiles work. Many are also slow to build, huge, and quietly insecure. These are the mistakes I see most often, and each one is a one-line fix.
1. Using the latest tag
FROM node:latest Your build changes the day upstream releases a new major version. Pin a version: FROM node:22-alpine.
2. Copying everything before installing dependencies
COPY . . RUN npm ci Any source change invalidates the cache, so dependencies reinstall every build. Copy the manifests first:
COPY package*.json ./ RUN npm ci COPY . . 3. Running as root
Containers run as root by default. Add a user and switch to it:
RUN adduser -D app USER app 4. Not cleaning package caches
apt-get update and install in separate RUN lines leaves stale caches in layers. Combine them and clean up:
RUN apt-get update && apt-get install -y --no-install-recommends curl \ && rm -rf /var/lib/apt/lists/* 5. Shipping build tools in the final image
Use multi-stage builds so compilers and dev dependencies never reach production:
FROM node:22 AS build WORKDIR /app COPY . . RUN npm ci && npm run build FROM node:22-alpine COPY --from=build /app/dist ./dist CMD ["node", "dist/server.js"] 6. Secrets in ENV or ARG
Anything in an ENV line or a build ARG can be read from the image history. Use BuildKit secrets or inject at runtime.
7. Missing .dockerignore
Without one, node_modules, .git and local .env files go into the build context. A short .dockerignore speeds builds and avoids leaking files.
Lint yours in the browser
I made a free Dockerfile linter that flags these patterns without sending your file anywhere: paste a Dockerfile and read the findings.
Try it: Dockerfile Linter
Related: Dockerfile generator and docker-compose generator. All 590+ tools at webdevtoolbox.com run client-side.


