Container Image Hardening: Security Without CI Slowdown
Reduce container attack surface through intelligent hardening techniques that integrate seamlessly into your CI pipeline without adding build time.
Your container images are a frequent target. Bloated base images, unnecessary packages, and unvetted dependencies create unnecessary risk—and every scan and rebuild steals CI minutes. The challenge isn't whether to harden; it's doing it efficiently.
Container hardening doesn't require lengthy scans or architectural overhauls. With the right approach, you can strip vulnerabilities from your images and ship faster.
Start with Minimal Base Images
The first rule of hardening is removing what you don't need. A standard Ubuntu or CentOS base image carries 400+ packages you'll never use. Each package is a potential vulnerability vector.
Switch to purpose-built minimal images:
dockerfile# Before: 77MB, 100+ packages FROM ubuntu:22.04 # After: 5MB, only what you need FROM gcr.io/distroless/base-debian11 COPY --from=builder /app/binary /app/ ENTRYPOINT [\"./app\"]
Distroless images from Google and Alpine Linux variants cut size by 90% and eliminate entire categories of vulnerabilities—no package manager, no shell, no room for privilege escalation. Build time impact: negligible. Security gain: substantial.
Multi-Stage Builds: Separation Without Overhead
Multi-stage builds are your efficiency multiplier. Compile in a full environment, ship only the artifact. This isn't new, but most teams aren't using it effectively.
dockerfile# Stage 1: Builder FROM golang:1.21-alpine AS builder WORKDIR /src COPY . . RUN go build -o app . # Stage 2: Runtime (6MB instead of 400MB) FROM gcr.io/distroless/base-debian11 COPY --from=builder /src/app / USER nonroot ENTRYPOINT [\"./app\"]
The builder stage can have build tools, dev headers, and test frameworks. None of that reaches production. Your final image contains only compiled binaries and runtime dependencies. Build time stays constant; image size and surface area shrink dramatically.
Automate Hardening in Your Pipeline
Vulnerability Scanning
Integrate lightweight scanning that doesn't block your pipeline—at least not initially. Tools like Trivy and Grype complete full image scans in seconds:
bash#!/bin/bash # Run in CI, fail on critical/high only trivy image --severity HIGH,CRITICAL --exit-code 1 $IMAGE_NAME
Scan early, fail fast on critical vulnerabilities, but allow lower-severity findings to pass. You'll iterate toward a clean baseline without gridlock.
Non-Root Users
Force containers to run as non-root. This eliminates an entire class of privilege escalation attacks and requires almost no overhead:
dockerfileRUN useradd -u 10001 -m appuser USER appuser ENTRYPOINT [\"./app\"]
Add this to every Dockerfile. Kubernetes will enforce it anyway; doing it in the image prevents accidental overrides.
Read-Only Filesystems
Configure your containers to run with read-only root:
yaml# In your deployment manifest securityContext: readOnlyRootFilesystem: true allowPrivilegeEscalation: false capabilities: drop: - ALL
This requires writable temp storage (emptyDir volumes), but prevents runtime modifications that could hide malware or exfiltrate data.
Make It Part of Your Standard
The teams shipping secure containers quickly are the ones who automated hardening into their standard templates. Create a base Dockerfile template for your organization—minimal image, non-root user, multi-stage build, security context defaults. New projects inherit security from day one.
At LavaPi, we've seen teams cut both image size and vulnerability count by 75% within two sprints by applying these practices consistently. The key is treating hardening as a design constraint, not a post-build remediation.
The Bottom Line
Container hardening doesn't require choosing between speed and security. Minimal base images, multi-stage builds, and automated scanning integrate into your existing CI without adding meaningful overhead. Start with distroless images and non-root users this week. Your images will be smaller, faster to push, and orders of magnitude harder to exploit.
LavaPi Team
Digital Engineering Company