01 / 20

Container Security
& Isolation

Understand how containers package applications, how they achieve isolation, and why their security model fundamentally differs from virtual machines.

Cyber Security

What Is a Container?

A container is a lightweight, standalone package that bundles an application with everything it needs to run: code, runtime, libraries, and configuration files. Unlike installing software directly on your computer, containers create a consistent, reproducible environment that works the same everywhere.

Key Terminology

Image
A read-only template containing the application and its dependencies. It is the blueprint from which containers are created.
Container
A running instance of an image. Each container has its own filesystem, processes, and network stack.
Host OS
The operating system running on the physical or virtual machine. All containers on a host share its kernel.

Why Containers Matter for Security

Modern applications are built from dozens of microservices: small, independent components that each run in their own container. This architecture creates a larger attack surface, but also enables powerful security strategies.

1
Reduced blast radius: If one container is compromised, the attacker is confined to that container's limited environment.
2
Immutable deployments: Containers are rebuilt from images rather than patched in place, reducing configuration drift.
3
Rapid patching: Vulnerable images can be replaced in seconds, not hours, dramatically shrinking the window of exposure.

The Shared Kernel Model

Hardware Host OS Kernel (Shared) Container Runtime (Docker Engine / containerd) Container A App + Libs Bins / Config Namespaces + cgroups Container B App + Libs Bins / Config Namespaces + cgroups Container C App + Libs Bins / Config Namespaces + cgroups

Namespaces & cgroups

Linux containers rely on two core kernel mechanisms to create isolation. Together, they define what a container can see and what resources it can use.

Namespaces
Control visibility: each container gets its own view of process IDs, network interfaces, mount points, hostnames, and user IDs.
cgroups
Control resources: limit CPU time, memory usage, disk I/O, and network bandwidth so one container cannot starve others.

Virtual Machine Architecture

Hardware Host OS Hypervisor (VMware / KVM / Hyper-V) Virtual Machine A Guest OS (Full Kernel) App 1 + Libs App 2 + Libs Virtual Hardware (vCPU, vRAM, vNIC) Virtual Disk Virtual Machine B Guest OS (Full Kernel) App 3 + Libs Virtual Hardware (vCPU, vRAM, vNIC) Virtual Disk

Containers vs. Virtual Machines

Containers Virtual Machines
Kernel Shared with host Own guest kernel
Isolation Mechanism Namespaces + cgroups Hypervisor + hardware
Boot Time Milliseconds Seconds to minutes
Size MBs (image layers) GBs (full OS disk)
Isolation Strength Process-level (weaker) Hardware-level (stronger)
Density Hundreds per host Tens per host
Attack Surface Shared kernel exposes risk Smaller (hypervisor only)

Isolation Boundaries in Depth

Linux Namespace Isolation PID Namespace Container sees only its own processes Net Namespace Own IP address, ports, routing table Mount Namespace Own filesystem view and mounts User Namespace Maps root inside to non-root outside UTS Namespace Own hostname and domain name IPC Namespace Isolated shared memory + semaphores cgroups: CPU, Memory, I/O, Network Limits

The Container Escape Problem

Because containers share the host kernel, a vulnerability in the kernel itself can allow an attacker to "escape" the container and gain access to the host system or other containers. This is the fundamental security limitation of containers compared to VMs.

Shared Host Kernel Container A Secure Container B Compromised Container C Secure Kernel exploit Lateral movement to other containers

Defense in Depth for Containers

1
Minimal base images: Use slim or "distroless" images to reduce the number of installed packages an attacker could exploit.
2
Run as non-root: Configure containers to run as an unprivileged user. Never use --privileged mode in production.
3
Read-only filesystems: Mount the container's filesystem as read-only to prevent an attacker from writing malicious files.
4
Seccomp & AppArmor: Use kernel security modules to restrict which system calls a container can make.
5
Image scanning: Scan images for known vulnerabilities (CVEs) before deployment using tools like Trivy or Snyk.

Security Checklist: Step by Step

1
Choose a minimal base image. Start from a trusted, slim image (e.g., Alpine Linux at ~5MB) rather than a full OS.
2
Scan the image for vulnerabilities. Run a CVE scanner and fix or replace any packages with critical or high severity issues.
3
Set a non-root user. Add a USER directive in your Dockerfile so the application never runs as root inside the container.
4
Drop unnecessary capabilities. Use --cap-drop=ALL and only add back the specific capabilities your application truly needs.
5
Enable read-only filesystem. Use --read-only and mount writable volumes only for directories that need writes (e.g., /tmp).
6
Apply network policies. Restrict which containers can talk to each other. Deny all traffic by default, then allow only what is needed.

Example 1: Insecure vs. Secure Image

✕ Insecure
Base: ubuntu:latest (188MB)
Runs as: root
Installs: curl, wget, vim, git
Filesystem: writable
Capabilities: all defaults
✓ Secure
Base: alpine:3.19 (5MB)
Runs as: appuser (UID 1001)
Installs: only app runtime
Filesystem: read-only
Capabilities: all dropped

Example 2: Network Isolation

Frontend Network Web Server Load Balancer Backend Network API Service Auth Service Database Network PostgreSQL Redis Cache ✕ Blocked Attacker?

Example 3: Privilege Escalation

This scenario shows how running a container as root with --privileged mode creates a direct path to host compromise. Compare with a hardened configuration.

✕ Privileged Container
1. Attacker exploits app vulnerability
2. Gets root shell inside container
3. Mounts host filesystem via /dev
4. Writes SSH key to host /root/.ssh
5. Full host access
✓ Hardened Container
1. Attacker exploits app vulnerability
2. Gets shell as non-root user
3. Cannot mount devices (no capability)
4. Filesystem is read-only
5. Attack contained

Common Mistakes

✕ Running as Root
Many default Dockerfiles run processes as root. If the app is compromised, the attacker has root-level access inside the container.
✕ Using :latest Tag
The :latest tag is mutable and unpredictable. Pin images to specific versions so you know exactly what code and dependencies are running.
✕ Exposing the Docker Socket
Mounting /var/run/docker.sock into a container gives it full control over the Docker daemon, equivalent to host root access.

Summary: Isolation Landscape

Container VM Bare Metal
Isolation Level Process Hardware None
Overhead Very Low Moderate None
Kernel Shared Dedicated Shared (one app)
Escape Risk Kernel vulns Hypervisor vulns N/A
Density High Medium 1 per machine
Best For Microservices Multi-tenant Single workloads

Question 1 of 5

What is the primary reason container isolation is considered weaker than VM isolation?

Question 2 of 5

Which two Linux kernel features are the foundation of container isolation?

Question 3 of 5

A developer runs a container with --privileged mode enabled. What security consequence does this have?

Question 4 of 5

Which practice best reduces the attack surface of a container image?

Question 5 of 5

Why do many cloud providers run containers inside virtual machines rather than directly on bare metal?