Best Practice

Container Image Hardening Guide

The Container Image Hardening Guide outlines essential steps to build minimal, non-root, signed container images with SBOMs. By following these best practices, teams can significantly reduce security risks, streamline compliance, and enhance their migration processes across various platforms, ensuring a robust and trustworthy deployment environment.

Organization
CIS & Docker
Published
May 18, 2021

Container Image Hardening Guide

What This Best Practice Entails and Why It Matters

Container images are the building blocks of modern application deployment. However, they can also be a significant security risk if not properly hardened. This best practice focuses on creating minimal, non-root, signed container images that include Software Bill of Materials (SBOMs).

Hardening your container images helps to:

  • Reduce Attack Surface: By minimizing the components in a container, you limit the potential vulnerabilities.
  • Enhance Security: Non-root images minimize the risk of privilege escalation attacks.
  • Improve Compliance: Signed images provide traceability and accountability for software components.

Step-by-Step Implementation Guidance

  1. Start with a Minimal Base Image: Use a lightweight image like Alpine or Distroless to reduce the number of packages.

    • Example: Instead of FROM ubuntu:latest, use FROM alpine:latest.
  2. Run as a Non-Root User: Specify a non-root user in your Dockerfile.

    RUN addgroup -S myuser && adduser -S myuser -G myuser
    USER myuser
    
  3. Limit Installed Packages: Only install the packages essential for your application. Use multi-stage builds to copy only required artifacts.

    • Example:
    FROM golang:alpine AS builder
    WORKDIR /app
    COPY . .
    RUN go build -o myapp
    
    FROM alpine:latest
    COPY --from=builder /app/myapp /usr/local/bin/myapp
    
  4. Use Image Signing: Implement image signing to verify the integrity of your images. Tools like Docker Content Trust can be used.

    • Enable Docker Content Trust: export DOCKER_CONTENT_TRUST=1
  5. Generate an SBOM: Use tools like Syft or CycloneDX to generate a Software Bill of Materials for your container images. This provides an inventory of components and can help in compliance audits.

    • Example: syft myimage:latest -o json > sbom.json
  6. Regularly Update Images: Keep your base images and dependencies up to date to mitigate known vulnerabilities.

Common Mistakes Teams Make When Ignoring This Practice

  • Using Default Root User: Running containers as root can lead to privilege escalation and other security risks.
  • Overly Complex Images: Including extraneous packages and dependencies increases vulnerabilities.
  • Neglecting Updates: Failing to regularly update images can result in exposure to known security issues.
  • Ignoring SBOMs: Not generating SBOMs can lead to a lack of visibility into components, complicating compliance and security audits.

Tools and Techniques That Support This Practice

  • Docker: The primary tool used for containerization.
  • Syft: A tool for generating SBOMs from container images.
  • Docker Content Trust: Enables image signing for verification.
  • Trivy: A vulnerability scanner that can analyze images for known security issues.
  • Docker Bench for Security: A script that checks for dozens of common best practices around deploying Docker containers in production.

How This Practice Applies to Different Migration Types

  • Cloud Migration: When moving to cloud services, hardening images ensures that only secure, verified containers are deployed.
  • Database Migration: Containers used for database services should also follow hardening practices to prevent unauthorized access.
  • SaaS Migration: For SaaS platforms, using hardened images can protect sensitive data and improve compliance.
  • Codebase Migration: When migrating code, ensure that the container images used for deployment are secure and minimal.

Checklist or Summary of Key Actions

  • Start with a minimal base image
  • Run containers as a non-root user
  • Limit installed packages to essentials only
  • Implement image signing
  • Generate and maintain an SBOM
  • Regularly update images to address vulnerabilities

By following these steps, you can ensure a secure and efficient containerization process that enhances the overall integrity of your applications.