Container Security

Difficulty: Advanced

Question

What are the most important security best practices for building and running Docker containers in production?

Answer

Container security comes down to shrinking what an attacker can reach and limiting what they can do if they get in. At build time that means minimal, pinned base images, regular vulnerability scanning and never baking secrets into layers. At runtime it means running as a non-root user, dropping Linux capabilities, using a read-only root filesystem, setting resource limits and never giving containers --privileged or the Docker socket.

Build time: what goes into the image

  • Minimal base images: Alpine, -slim, distroless or hardened minimal images (Chainguard, Docker Hardened Images) contain fewer packages, so fewer CVEs and no shell or package manager for an attacker to use.
  • Pin versions: use specific tags and ideally digests (node:24-alpine@sha256:...) so a rebuild does not silently pull different code. Rebuild regularly to pick up patches.
  • Scan images: docker scout cves, Trivy or Grype in CI; fail the pipeline on critical vulnerabilities that have a fix.
  • No secrets in layers: ENV API_KEY=..., ARG values and copied .env files are visible in docker history or the image filesystem even if a later layer deletes them. Use RUN --mount=type=secret,id=npmrc during builds.
  • Multi-stage builds so compilers and source code never reach the runtime image.
  • Supply chain: generate SBOMs and provenance attestations (docker buildx build --sbom=true --provenance=true) and sign images (for example with cosign) so you can prove what is running.

Run time: what the container is allowed to do

ControlHowWhy
Non-root userUSER 10001 in Dockerfile, or --userRoot in a container is root on the kernel's view of many resources
Drop capabilities--cap-drop ALL --cap-add NET_BIND_SERVICERemoves powers like raw sockets or changing file ownership
No privilege escalation--security-opt no-new-privilegesBlocks setuid binaries from gaining root
Read-only rootfs--read-only --tmpfs /tmpAttackers cannot drop tools or modify binaries
Resource limits--memory 512m --cpus 1 --pids-limit 200Contains fork bombs and noisy neighbours
No privileged modeAvoid --privilegedIt disables almost all isolation
No Docker socketNever mount /var/run/docker.sockSocket access is equivalent to root on the host

Host and daemon level

  • Keep the kernel, Docker Engine and runc updated; container escapes usually exploit kernel or runtime bugs.
  • Consider rootless Docker or user namespace remapping (userns-remap) so container root maps to an unprivileged host UID.
  • Keep default seccomp and AppArmor or SELinux profiles enabled.
  • Restrict who can run docker commands; membership in the docker group is effectively root.

How this maps to real platforms

For a fintech handling UPI payments, security reviews and compliance audits typically expect these controls: non-root containers, image scanning in CI, secrets from a vault rather than images, and least privilege. In Kubernetes the same ideas appear as securityContext fields (runAsNonRoot, readOnlyRootFilesystem, allowPrivilegeEscalation: false, capabilities.drop: ["ALL"]) enforced through Pod Security Standards.

Defense in depth for containers. Each layer limits damage if the one above it is breached.
  1. Image: minimal, pinned, scanned: Fewer packages and known CVEs to exploit.
  2. Build: secrets via BuildKit mounts: No credentials stored in layers or history.
  3. Process: non-root, no-new-privileges: Compromise does not yield root.
  4. Container: dropped caps, read-only fs: Limits what a compromised process can change.
  5. Host: patched kernel, rootless or userns: Makes container escapes harder and less damaging.

Code examples

Hardened Dockerfile with a build secret

# syntax=docker/dockerfile:1
FROM node:24-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
# Token is mounted only for this RUN, never stored in a layer
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc \
    npm ci --omit=dev

FROM node:24-alpine
WORKDIR /app
COPY --from=deps --chown=node:node /app/node_modules ./node_modules
COPY --chown=node:node dist ./dist
USER node
CMD ["node", "dist/server.js"]

# Build: docker build --secret id=npmrc,src=$HOME/.npmrc -t api .

The private registry token is available only during npm ci, and the final image runs as the unprivileged node user.

Locked-down docker run and a scan

docker run -d --name api \
  --user 1000:1000 \
  --read-only --tmpfs /tmp \
  --cap-drop ALL \
  --security-opt no-new-privileges \
  --memory 512m --cpus 1 --pids-limit 200 \
  -p 3000:3000 api:1.0

# Scan for known vulnerabilities
docker scout cves api:1.0
# or
trivy image --severity HIGH,CRITICAL api:1.0

Each flag removes a privilege an attacker could use; scanning catches known CVEs before deployment.

Key points

  • Never run as root; set USER in the Dockerfile.
  • Secrets in ENV, ARG or copied files remain visible in image history; use BuildKit secret mounts or runtime secret stores.
  • Drop all capabilities, add back only what is needed, and enable no-new-privileges.
  • Avoid --privileged and never mount the Docker socket into app containers.
  • Use minimal pinned base images and scan them in CI.

Common follow-up questions

  • What is a distroless image and what are its trade-offs?
  • How do you pass secrets securely to a container at build time and at runtime?
  • Why is mounting /var/run/docker.sock dangerous?
  • What are Linux capabilities and which ones does Docker grant by default?
  • How do these controls map to Kubernetes securityContext?

Interview tips

  • Structure your answer as build time vs run time vs host; it sounds organised and complete.
  • Use the phrase principle of least privilege and back it with specific flags like --cap-drop ALL.
  • Mention a supply chain control such as scanning in CI or SBOMs; it signals awareness of modern security expectations.

Concepts covered

Non-root User, Image Scanning, Linux Capabilities, BuildKit Secrets, Read-only Root Filesystem

More Docker & Containers questions