Best Practices for Securing Docker Containers in Production Environments

Implement defense-in-depth by combining minimal image builds, automated vulnerability scanning with Trivy, CIS benchmark auditing via docker-bench-security, and runtime hardening flags like --read-only and --cap-drop ALL.

The trimstray/the-book-of-secret-knowledge repository curates a comprehensive collection of Docker security resources under its Containers/Orchestration and Manuals/Tutorials/Best Practices sections. These resources outline best practices for securing Docker containers in production environments, providing a security-by-design pipeline that spans build-time hardening, image trust, host auditing, and runtime confinement.

Baseline Host and Daemon Auditing

According to the repository's README.md at line 811, the first step in securing Docker containers in production is auditing the host and Docker daemon against the CIS Docker Benchmark. The docker-bench-security script automates this process, detecting misconfigurations such as missing user namespaces, insecure daemon socket exposure, and overly permissive capabilities.

Run this audit periodically to ensure your infrastructure maintains compliance:

docker run -it --net host --pid host --userns host --cap-add audit_control \
  -e DOCKER_CONTENT_TRUST=$DOCKER_CONTENT_TRUST \
  -v /var/lib:/var/lib \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v /usr/lib/systemd:/usr/lib/systemd \
  -v /etc:/etc --label docker_bench_security \
  docker/docker-bench-security

Image Vulnerability Scanning and Trust

Automated Scanning with Trivy in CI/CD

The repository emphasizes scanning images with Trivy before they reach production (referenced at line 12). Trivy finds CVEs in OS packages and known vulnerable application dependencies early in CI/CD, allowing pipelines to fail on high-severity findings.


# Scan the image and fail if any HIGH severity CVEs exist

trivy image --severity HIGH,CRITICAL myorg/app:1.0.0 || exit 1

Content Signing with Harbor and Notary

To guarantee that only trusted, tamper-free images are deployed, the repository recommends storing signed images in Harbor, which provides Notary-based content signing and role-based access control (line 13).


# Push and sign the image

docker push myorg/app:1.0.0
notary addhash myorg/app:1.0.0 <hash>
notary publish

Build-Time Security Patterns

The docker_practice repository (line 22) and the dockerfiles collection (line 25) demonstrate build-time best practices for securing Docker containers in production. These include using minimal base images, multi-stage builds to reduce attack surface, and running as non-root users.

Multi-Stage Builds with Minimal Base Images


# ---------- Multi‑stage build ----------

FROM golang:1.22-alpine AS builder
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /app

# ---------- Lightweight runtime image ----------

FROM gcr.io/distroless/static:nonroot

# Use a non‑root user created by distroless (uid 65532)

COPY --from=builder /app /app
ENTRYPOINT ["/app"]

Runtime Hardening and Confinement

The repository's curated docker-cheat-sheet (line 20) outlines essential runtime flags for securing Docker containers in production environments. These flags reduce the attack surface of each container while it runs.

Essential Runtime Security Flags


# Run the container with strongest defaults

docker run -d \
  --read-only \
  --cap-drop ALL \
  --security-opt no-new-privileges \
  --security-opt seccomp=./seccomp-profile.json \
  --user 65532:65532 \
  myorg/app:1.0.0

Explanation of key flags:

  • --read-only: Makes the container's root filesystem immutable, preventing accidental writes.
  • --cap-drop ALL: Removes Linux capabilities, leaving only the minimal set required by the app.
  • --security-opt no-new-privileges: Stops the process from gaining additional privileges via set-uid binaries.
  • --security-opt seccomp=...: Enforces a custom seccomp profile that blocks dangerous syscalls.
  • --user 65532:65532: Runs the process as a non-root user defined by the base image.

Kubernetes-Specific Hardening

When containers are orchestrated by Kubernetes, the repository points to k8s-security and kubernetes-production-best-practices (lines 29-31) for pod-level hardening. These resources cover PodSecurityPolicy, network policies, and RBAC to extend container security to the cluster level.

Curated Resource Collections

The repository's README.md references several community-vetted collections that provide ongoing guidance on securing Docker containers in production:

  • awesome-docker (line 21): Community-vetted guidelines, tooling, and patterns for logging, monitoring, and secret management.
  • docker_practice (line 22): Reproducible setups demonstrating least-privilege containers, multi-stage builds, and CI integration.
  • dockerfiles (line 25): Real-world Dockerfiles following security conventions such as minimal base images and non-root users.

Summary

  • Audit hosts and daemons against CIS benchmarks using docker-bench-security to catch misconfigurations before deployment.
  • Integrate Trivy into CI/CD pipelines to block images with high-severity CVEs from reaching production.
  • Store and sign images in Harbor with Notary to ensure only trusted artifacts are deployed.
  • Build minimal images using multi-stage builds and non-root users as demonstrated in the dockerfiles and docker_practice repositories.
  • Harden runtime configurations with flags like --read-only, --cap-drop ALL, and --security-opt no-new-privileges as documented in the docker-cheat-sheet.
  • Extend security to Kubernetes clusters using k8s-security and kubernetes-production-best-practices for pod-level policies and network segmentation.

Frequently Asked Questions

How does docker-bench-security help secure Docker containers in production?

The docker-bench-security script audits the host and Docker daemon against the CIS Docker Benchmark recommendations, detecting misconfigurations such as missing user namespaces, insecure daemon socket exposure, and overly permissive capabilities. According to the repository's README.md at line 811, this should be the first step in any production hardening strategy to establish a secure baseline.

The repository recommends integrating Trivy into CI/CD workflows to scan images before they reach production (referenced at line 12). Trivy identifies CVEs in OS packages and application dependencies, allowing pipelines to fail on high-severity findings. This build-time vetting ensures vulnerable images never deploy to production environments.

Which runtime flags are essential for securing Docker containers?

According to the docker-cheat-sheet referenced at line 20, essential runtime flags include --read-only for immutable root filesystems, --cap-drop ALL to remove unnecessary Linux capabilities, --security-opt no-new-privileges to prevent privilege escalation, and --user to enforce non-root execution. These flags collectively minimize the attack surface of running containers.

How should image trust and integrity be managed in production?

The repository advocates using Harbor as a secure registry with Notary-based content signing (line 13). This setup guarantees that only signed, tamper-free images are deployed. Combined with role-based access control, Harbor ensures that image provenance is verified before containers run in production environments.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →