Menu

7 Dockerfile Mistakes That Make Builds Slow, Huge and Insecure
📰

7 Dockerfile Mistakes That Make Builds Slow, Huge and Insecure

DEV Community: docker·Sanjaya Sahu·7 days ago
#ejwa7hkY
#devtodocker#devto#dev#build#enter
Long article (331 words) — no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.