Skip to content
Blog

Common Docker Beginner Mistakes and How to Avoid Them

Identify and fix 5 common Docker anti-patterns: running processes as root, state in writable layers, abusing latest tags, bloated images, and missing .dockerignore.

Published on • November 17, 2026

AI Assistant

Containerization simplifies application packaging, but adopting Docker without understanding container primitives can introduce major security vulnerabilities, slow build performance, and operational instability.

This article highlights five common beginner Docker mistakes and provides practical code solutions to fix them.


1. Running Container Processes as Root

By default, Docker containers run processes as root (UID 0). If an application contains a remote code execution (RCE) vulnerability, an attacker gaining shell access inside a root container can attempt kernel privilege escalation to compromise the underlying host system.

# ❌ ANTI-PATTERN: Runs process as root by default
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
CMD ["node", "server.js"]

The Solution: Create and Use Unprivileged Users

Switch to an explicit non-root user before declaring runtime execution:

# ✅ RECOMMENDED: Creates and switches to an unprivileged system user
FROM node:22-alpine
WORKDIR /app

# Create unprivileged user group and user
RUN addgroup -S appgroup && adduser -S appuser -G appgroup

COPY package*.json ./
RUN npm ci --only=production

# Copy application files with proper user ownership
COPY --chown=appuser:appgroup . .

USER appuser
EXPOSE 3000
CMD ["node", "server.js"]

2. Storing Application Data in the Writable Container Layer

New developers often expect files written inside a running container to persist automatically across updates. However, deleting a container (docker rm) destroys its writable layer and all data stored within it. Writing data to the container layer also degrades performance due to Union File System Copy-on-Write (CoW) overhead.

# ❌ ANTI-PATTERN: Writes database files directly inside container filesystem
docker run -d --name my-db postgres:16

The Solution: Use Explicit Docker Volumes

Decouple state from container execution using named volumes:

# ✅ RECOMMENDED: Persists database data in a daemon-managed volume
docker run -d \
  --name my-db \
  -v postgres_data:/var/lib/postgresql/data \
  postgres:16-alpine

3. Abusing the :latest Tag

Relying on FROM python:latest or image: nginx causes non-deterministic deployments. An upstream image update can silently introduce breaking changes or security vulnerabilities into your CI pipeline without any code changes on your part.

# ❌ ANTI-PATTERN: Unpredictable base image
FROM python:latest

The Solution: Pin Specific Tags or SHA-256 Checksums

# ✅ RECOMMENDED: Explicit version pinning with cryptographic checksum
FROM python:3.12.3-slim-bookworm@sha256:a6b2518e001a1d9a24d08129dd9dd2b6832db7bc305373a6a8c4f9eb6e3a7bf2

4. Building Bloated Images (Gigabyte Container Syndrome)

Including build SDKs, compilers (gcc, make), git histories, and full Linux OS distributions in runtime images results in 1 GB+ container sizes. Large images slow down deployment pipelines, consume bandwidth, and expand your attack surface.

# ❌ ANTI-PATTERN: Single-stage build leaving build tools in final image
FROM golang:1.22
WORKDIR /app
COPY . .
RUN go build -o server main.go
CMD ["./server"]  # Image size: ~850 MB

The Solution: Multi-Stage Builds

Separate the build phase from the final runtime image using multi-stage builds:

# ✅ RECOMMENDED: Multi-stage build producing lean binary image
# Stage 1: Build Environment
FROM golang:1.22-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o server main.go

# Stage 2: Minimal Runtime (Scratch / Alpine)
FROM alpine:3.20
RUN adduser -D appuser
WORKDIR /app
COPY --from=builder /app/server .
USER appuser
CMD ["./server"]  # Final image size: ~15 MB

5. Omitting .dockerignore Files

Without a .dockerignore file, running COPY . . sends your entire local working directory—including .git histories, local node_modules, .env secrets, and temporary build caches—to the Docker daemon as the build context. This invalidates layer caching and risks leaking credentials into image layers.

Build context size without .dockerignore: 450 MB (Slow transfer)
Build context size with .dockerignore:    1.2 MB (Instant build)

The Solution: Add a Standard .dockerignore File

Create a .dockerignore file in your root project directory:

# .dockerignore
.git
.github
node_modules
npm-debug.log
build
dist
.env*
*.md
Dockerfile
docker-compose*.yml

Summary Best Practices Guide

AreaAnti-PatternCorrect Practice
PrivilegesRunning container processes as default rootCreate unprivileged user (USER appuser)
StorageWriting database files inside container layerMount persistent Docker Volumes (-v data:/path)
VersioningUsing image:latest tagsPin explicit semantic tags and SHA-256 digests
Image SizeSingle-stage builds with compilers includedUse multi-stage builds (FROM ... AS builder)
Build ContextSending .git and node_modules to daemonMaintain an explicit .dockerignore file

Takeaway: Avoiding common Docker mistakes requires adopting non-root user privileges, isolating persistent data in volumes, pinning base image digests, utilizing multi-stage builds, and filtering build contexts with .dockerignore.


Sources