Security Best Practices
Non-root users, read-only filesystems, minimal images, and secret management
Skills taught: docker.security, docker.non-root, docker.read-only, docker.minimal-images, docker.secret-management
Why container security matters
By default, processes inside Docker containers run as root. If your application has a vulnerability that lets an attacker execute code inside the container, root access inside means they can potentially escape the container, read other containers' data, or mount the host filesystem. Container security is about reducing the blast radius of a breach through multiple layers of defense.
Run as a non-root user
FROM node:20-alpine
# Create a non-root user and group
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
# Give ownership of /app to appuser BEFORE switching
RUN chown -R appuser:appgroup /app
# Switch to non-root user — all subsequent commands and the running container
# will use this user
USER appuser
EXPOSE 3000
CMD ["node", "server.js"]
# Verify: docker run --rm myapp whoami → 'appuser' (not root)
Use minimal base images
# Larger images = more packages = more CVEs to patch
# Scan images for vulnerabilities:
docker scout cves node:20 # Docker's built-in scanner
docker scout cves node:20-slim
docker scout cves node:20-alpine
# alpine typically has 10-20x fewer CVEs than the full Debian image
# Distroless images (Google): no shell, no package manager
# Even harder to exploit — no /bin/bash for an attacker to use
# FROM gcr.io/distroless/nodejs20-debian12
# Good → Better → Best:
# node:20 → node:20-slim → node:20-alpine → distroless
# Regularly update base images
docker pull node:20-alpine # get latest patches
docker build --no-cache -t myapp . # rebuild without cache
Read-only filesystem and secret hygiene
docker run \
--read-only \
--tmpfs /tmp \
--tmpfs /app/logs \
--cap-drop ALL \
--cap-add NET_BIND_SERVICE \
--security-opt no-new-privileges \
--user 1001:1001 \
myapp
# --read-only: the container filesystem is read-only
# attackers can't write malware, change configs, etc.
# --tmpfs /tmp: writable temp dir in memory (since we're read-only)
# --cap-drop ALL: drop ALL Linux capabilities
# --cap-add NET_BIND_SERVICE: re-add ONLY what's needed (bind ports <1024)
# --security-opt no-new-privileges: process can't escalate privileges
# --user 1001:1001: run as a non-root UID
# ❌ WRONG — secret baked into image layer (visible in docker history)
RUN pip install -r requirements.txt
RUN npm config set registry https://user:TOKEN@registry.npmjs.org
# ❌ WRONG — even if you delete in next layer, it's in the history
RUN curl -H 'Authorization: Token $API_KEY' ...
RUN unset API_KEY # too late — it's in the previous layer
# ✅ RIGHT — use BuildKit secrets (never stored in layers)
# syntax=docker/dockerfile:1.4
FROM node:20-alpine
# --mount=type=secret mounts the secret file only during this RUN step
# and it is NOT stored in the image layer
RUN --mount=type=secret,id=npm_token \
NPM_TOKEN=$(cat /run/secrets/npm_token) \
npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN && \
npm install && \
npm config delete //registry.npmjs.org/:_authToken
# Build with:
docker build --secret id=npm_token,src=.npm-token -t myapp .
Frequently Asked Questions
How do I scan my images for known vulnerabilities?
Several options: (1) 'docker scout cves <image>' — built into Docker Desktop; (2) Trivy (aquasecurity/trivy) — free, fast, widely used: 'trivy image myapp:latest'; (3) Snyk — integrates with GitHub and CI. Make scanning part of your CI pipeline so new vulnerabilities in base images are caught before deployment.