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
Enter fullscreen mode
Exit fullscreen mode
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
Enter fullscreen mode
Exit fullscreen mode
Any source change invalidates the cache, so dependencies reinstall every build. Copy the manifests first:
COPY package*.json ./ RUN npm ci COPY . .
Enter fullscreen mode
Exit fullscreen mode
3. Running as root
Containers run as root by default. Add a user and switch to it:
RUN adduser -D app USER app
Enter fullscreen mode
Exit fullscreen mode
4. Not cleaning package caches
apt-get update and install in separate RUN lines leaves stale caches in layers.…