Container Security and Image Scanning

The $50,000 AWS Bill: When Cryptominers Hijack Your Containers

Monday morning. I grabbed my coffee, opened my laptop, and nearly spat it out when I saw the AWS billing alert: $48,723.42 for the weekend. Our normal weekend bill? Maybe $2,000.

Someone had compromised one of our container images and deployed cryptomining malware across our entire Kubernetes cluster. The attacker exploited a vulnerability in our base imageβ€”a three-year-old version of Alpine Linux we'd been using since "the beginning." We'd never scanned it. We'd never updated it. It just... worked. Until it didn't.

The post-mortem revealed 523 critical vulnerabilities in that single base image. The attack vector? A vulnerable version of curl in the base layer, exploited through an exposed debugging endpoint we'd forgotten to remove.

That incident cost us $50,000 in compute charges, a week of incident response, mandatory security audits, and worst of allβ€”customer trust. But it taught me everything I know about container security.

This article covers the container security strategy I built from scratch after that disaster. We went from 500+ vulnerabilities per image to zero critical findings, from ad-hoc image building to a hardened, audited pipeline.

What You'll Learn

  • Container security fundamentals and the attack surface

  • Image scanning with Trivy, Clair, and Snyk

  • Building secure, minimal base images

  • Multi-stage builds for security

  • Container runtime security

  • Kubernetes security contexts and admission controls

  • Pod Security Standards implementation

The Container Security Threat Model

Containers introduce unique security challenges across multiple layers:

spinner

The container attack surface:

  • Application layer: Your code and dependencies

  • Image layer: Base images, OS packages

  • Build layer: Dockerfile, build process

  • Registry layer: Image storage, distribution

  • Runtime layer: Container engine, orchestrator

  • Host layer: Kernel, system calls

Image Scanning with Trivy

Trivy is my go-to scanner for containers. It's fast, accurate, and finds vulnerabilities in OS packages, application dependencies, and even IaC misconfigurations.

Basic Trivy Scanning

Advanced Trivy Configuration

Trivy in Pre-commit Hooks

Building Secure Base Images

The foundation of container security is choosing the right base image.

The Base Image Comparison

Multi-Stage Build for Security

Security-Hardened Dockerfile Best Practices

Container Runtime Security

Scanning images isn't enoughβ€”you need runtime protection too.

Kubernetes Security Contexts

Pod Security Standards

Network Policies

Admission Controllers

Prevent vulnerable containers from being deployed in the first place.

OPA Gatekeeper Policies

Kyverno Policy

Continuous Image Scanning in Registry

Don't just scan on buildβ€”scan continuously as new vulnerabilities are discovered.

Harbor Registry with Trivy

Runtime Threat Detection

Falco for Runtime Security

Best Practices

1. Minimize Base Images

Smaller images = smaller attack surface

2. Scan Everything, Everywhere

3. Never Run as Root

4. Use Read-Only Filesystems

5. Set Resource Limits

Key Takeaways

βœ… Scan images continuously - Not just at build time βœ… Minimize base images - Distroless or Alpine preferred βœ… Multi-stage builds - Keep build tools out of production βœ… Run as non-root - Always use unprivileged users βœ… Read-only filesystems - Prevent runtime modifications βœ… Admission controls - Block vulnerable pods before deployment βœ… Runtime monitoring - Detect threats in running containers

What's Next

Container images are built from code and dependencies. In the next article, we'll cover Software Bill of Materials (SBOM)β€”creating comprehensive inventories of everything in your software supply chain for compliance and security.


Next Article: Software Bill of Materials (SBOM) β†’


Part of the DevSecOps 101 Series

Last updated