Roadmap › Docker & Containers › Module: Production Practices
Lessons in this course (8)

6. Image size, security and best practices

Intermediate★ Must knowLesson 6 of 8
  1. Multi-stage builds; minimal base images (aspnet, chiseled, Alpine); pin versions (aspnet:10.0 or a digest).
  2. Run as non-root (USER app); read-only root filesystem where possible; drop Linux capabilities.
  3. No secrets in images (not in ENV, ARG or copied files — they stay in layer history). Inject at runtime via environment/secret stores (Key Vault, Kubernetes Secrets, Docker secrets); use BuildKit --mount=type=secret for build-time secrets.
  4. Order instructions from least to most frequently changing for cache hits; combine RUN steps and clean package caches in the same layer.
  5. .dockerignore to keep the build context small and avoid leaking files.
  6. Scan images in CI (Trivy, Docker Scout, Defender) and rebuild regularly to pick up base image patches.
  7. One process (concern) per container; log to stdout/stderr.
  8. Add HEALTHCHECK (Docker/Compose) or liveness/readiness probes (Kubernetes).
  9. Handle SIGTERM for graceful shutdown (ASP.NET Core does by default; keep the exec form).
  10. Sign images (Notation/cosign) and use immutable tags.