Difficulty: Advanced
What are the most important security best practices for building and running Docker containers in production?
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.
-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.node:24-alpine@sha256:...) so a rebuild does not silently pull different code. Rebuild regularly to pick up patches.docker scout cves, Trivy or Grype in CI; fail the pipeline on critical vulnerabilities that have a fix.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.docker buildx build --sbom=true --provenance=true) and sign images (for example with cosign) so you can prove what is running.| Control | How | Why |
|---|---|---|
| Non-root user | USER 10001 in Dockerfile, or --user | Root in a container is root on the kernel's view of many resources |
| Drop capabilities | --cap-drop ALL --cap-add NET_BIND_SERVICE | Removes powers like raw sockets or changing file ownership |
| No privilege escalation | --security-opt no-new-privileges | Blocks setuid binaries from gaining root |
| Read-only rootfs | --read-only --tmpfs /tmp | Attackers cannot drop tools or modify binaries |
| Resource limits | --memory 512m --cpus 1 --pids-limit 200 | Contains fork bombs and noisy neighbours |
| No privileged mode | Avoid --privileged | It disables almost all isolation |
| No Docker socket | Never mount /var/run/docker.sock | Socket access is equivalent to root on the host |
userns-remap) so container root maps to an unprivileged host UID.docker commands; membership in the docker group is effectively root.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.
# 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.
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.
Non-root User, Image Scanning, Linux Capabilities, BuildKit Secrets, Read-only Root Filesystem