Container in depth
Example: from source to a running container
Example: a hypothetical Python service is packaged with a multi-stage build. The first stage uses a full Python image to install dependencies into a virtual environment. The second stage starts from a slim or distroless base, copies only the virtual environment and the application code, sets a non-root user, and declares the start command. The resulting image is tagged with the Git commit, scanned, signed and pushed to a registry. On Kubernetes, the pod spec sets CPU and memory requests and limits, a read-only root file system, dropped Linux capabilities and a liveness and readiness probe. The same image runs unchanged in CI, staging and production.
Production practices for containers
- Small, known base images: fewer packages means fewer vulnerabilities to patch. Distroless, Alpine or vendor-maintained minimal images are common choices, each with trade-offs in debugging and compatibility.
- Pin by digest: tags like latest move; a digest identifies one exact image.
- Least privilege at runtime: non-root user, no privileged mode, seccomp profile set to RuntimeDefault, minimal capabilities. Kubernetes Pod Security Standards describe baseline and restricted levels.
- One process concern per container: sidecars for separate jobs such as log shipping or proxies.
- Configuration from outside: environment variables, config maps and secret stores, never baked into images.
- Graceful shutdown: handle SIGTERM so in-flight requests finish during rollouts and scale-downs.
Images are built with Docker BuildKit, Buildah or Kaniko, run by containerd or CRI-O, and stored in OCI registries such as Amazon ECR, Azure Container Registry, Google Artifact Registry or Harbor. Scanners such as Trivy and Grype report known vulnerabilities, Syft generates SBOMs, and Sigstore Cosign signs images so clusters can verify them before running.
How to evaluate your container estate
| Check | Question to answer |
| Image size and layers | Are build tools and caches leaking into production images? |
| Vulnerabilities | How many critical and high findings are fixable, and how old are they? |
| Provenance | Can you trace each running image to a commit and pipeline run? |
| Runtime posture | How many containers run as root or privileged? |
| Resource settings | Do requests reflect real usage? See rightsizing |
Rebuild images on a schedule, not only when code changes, so base image security fixes reach production. Automated tools such as Renovate or Dependabot can open pull requests when a new base image is published, and the normal pipeline then tests, scans and ships the rebuilt image like any other change.
Common mistakes
- Secrets copied into an image layer, where they remain even if a later layer deletes them.
- Treating containers like virtual machines: SSH inside, manual patching, long-lived state on the container file system.
- No memory limits, so one leaking container can starve a node.
- Base images that are never rebuilt, so fixes upstream never reach production.
Containers are built with tools such as Docker, scheduled by Kubernetes, observed at the kernel level with eBPF, and documented with an SBOM. We cover this work in Docker and containerization.