Docker Container Security Hardening: Distroless & Non-Root
Containers are the foundation of modern cloud deployment, but default container images often ship with bloated Linux OS distributions containing package managers (apt, apk), shells (bash, sh), and outdated system utilities. If a vulnerability is exploited in your application, attackers can leverage these built-in utilities to escalate privileges or perform lateral network movement.
This guide covers essential container security hardening practices: Multi-Stage Builds, Google Distroless Base Images, Non-Root Execution, and Read-Only Root Filesystems.
Multi-Stage Builds & Distroless Base Images
Multi-Stage Builds separate the build-time SDK environment (which requires compilers, build tools, and package managers) from the runtime environment. The final production image copies only compiled application artifacts into a minimal runtime image.
Distroless images (maintained by Google) contain only your application and its runtime dependencies (Node.js, Python, Java, or Go binaries). They contain no package managers, shell interpreters, or standard Unix utilities, eliminating 90%+ of common CVE vulnerabilities.
Quick reference
- Distroless images remove shell interpreters (
sh,bash), preventing attackers from executing web shell payloads. - Executing as
USER nonrootprevents container escape attacks from gaining root access on host Linux nodes. - Enforcing
readOnlyRootFilesystem: trueprevents malware from writing malicious scripts to the container disk.
Remember this
Using Distroless base images and non-root users eliminates the vast majority of container vulnerability attack vectors.
Container Vulnerability Scanning & Dropping Capabilities
Integrate automated container image vulnerability scanning (using tools like Trivy, Grype, or Docker Scout) into your CI/CD pipelines to catch vulnerable OS libraries before deployment.
Furthermore, drop all Linux Kernel Capabilities (capabilities.drop: ['ALL']) in your Kubernetes SecurityContext to enforce strict least-privilege container execution.
Quick reference
- Run
trivy image myrepo/app:latestin GitHub Actions to block builds containing Critical CVEs. - Mount ephemeral
tmpfsvolumes for temporary file processing when using read-only root filesystems. - Compare with Docker Multi-Stage Builds and Docker vs Kubernetes.
Remember this
Automated vulnerability scanning paired with dropped Linux capabilities creates a defensible container runtime posture.
Container Hardening Security Checklist
Follow this production checklist for hardening every container before shipping to Kubernetes or cloud servers.
For related guides, see our articles on CI/CD GitHub Actions Deployment and Kubernetes Request Flow.
Quick reference
- Check 1: Minimal Base Image (Use Distroless or Alpine Linux).
- Check 2: Non-Root Execution (
USER nonroot:nonrootorUSER 10001). - Check 3: Read-Only Root Filesystem (
readOnlyRootFilesystem: true). - Check 4: Dropped Linux Capabilities (
drop: ['ALL']).
Remember this
Adhering to minimal base images, non-root users, and read-only filesystems guarantees production container security.
Key takeaway
Hardening Docker containers with Distroless images, non-root users, and read-only filesystems turns containers into secure, unyielding production runtime environments.
Related Articles
Explore this topic