
Containerization Fundamentals
Security-focused overview of Docker and Kubernetes architecture, covering isolation mechanisms, security boundaries, and common misconfigurations from an offensive security perspective.
Introduction
Most applications now ship as containers, and a container is not a virtual machine. You need to know where the isolation actually sits to attack it or to harden it.
This note walks through the parts of Docker and Kubernetes that matter for security: where the isolation boundaries are and which misconfigurations break them.
Security perspective
Containers share the host kernel, so the threat model is different from a VM. One kernel vulnerability affects every container on the host.
Docker architecture
How containers work
Docker builds process isolation out of Linux kernel features:
| Feature | What it does |
|---|---|
| Namespaces | Give the container its own process tree, network stack, users and mount table |
| Cgroups | Cap CPU, memory and I/O |
| Union filesystems | Stack image layers so containers share read-only base layers |
| Capabilities | Split root's privileges into individual grants |
# View container namespaces
lsns -p $(docker inspect --format '{{.State.Pid}}' container_name)
# View cgroup limits
cat /sys/fs/cgroup/memory/docker/<container_id>/memory.limit_in_bytesDocker components
| Component | Function | Security relevance |
|---|---|---|
| Docker Daemon | Manages containers | Runs as root, API access = host compromise |
| Docker CLI | Client interface | Credentials stored in ~/.docker/config.json |
| containerd | Container runtime | Lower-level container management |
| runc | OCI runtime | Executes containers, subject to CVEs |
| Docker Registry | Image storage | Supply chain attacks, credential exposure |
Security boundaries
Docker isolation is weaker than VM isolation. Every container talks to the same kernel:
┌─────────────────────────────────────────────┐
│ Host OS │
│ ┌──────────────────────────────────────┐ │
│ │ Linux Kernel │ │
│ │ (Shared by all containers) │ │
│ └──────────────────────────────────────┘ │
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │Container1│ │Container2│ │Container3│ │
│ │ Namespace│ │ Namespace│ │ Namespace│ │
│ └──────────┘ └──────────┘ └──────────┘ │
└─────────────────────────────────────────────┘What follows from that:
- A kernel exploit hits every container on the host
- A container escape is a host compromise
- Root in the container is not root on the host, unless the configuration makes it so
Docker socket exposure
Look for the Docker socket (/var/run/docker.sock) first. Anyone who can talk to it controls the daemon:
# If mounted in container, full host compromise is possible
docker run -v /var/run/docker.sock:/var/run/docker.sock alpine
# From inside container with socket access
docker run -v /:/host -it alpine chroot /hostHigh risk
Don't mount the Docker socket into a container unless there is no other way. Socket access is root on the host.
Kubernetes architecture
Control plane components
| Component | Function | Security relevance |
|---|---|---|
| kube-apiserver | API endpoint | Primary attack target, RBAC enforcement |
| etcd | Cluster state store | Contains secrets, requires encryption |
| kube-scheduler | Pod placement | Influences where workloads run |
| kube-controller-manager | Cluster controllers | Service account token management |
| cloud-controller-manager | Cloud provider integration | Cloud credential access |
Worker node components
| Component | Function | Security relevance |
|---|---|---|
| kubelet | Pod management | Node-level API, sometimes left unauthenticated |
| kube-proxy | Network proxy | Service routing, network policies |
| Container runtime | Runs containers | containerd, CRI-O, Docker |
Kubernetes security model
┌─────────────────────────────────────────────────────────┐
│ Kubernetes Cluster │
│ ┌────────────────────────────────────────────────────┐ │
│ │ Control Plane │ │
│ │ ┌─────────┐ ┌────┐ ┌───────────┐ ┌──────────┐ │ │
│ │ │API Server│ │etcd│ │Scheduler │ │Controller│ │ │
│ │ └─────────┘ └────┘ └───────────┘ └──────────┘ │ │
│ └────────────────────────────────────────────────────┘ │
│ │
│ ┌────────────────────┐ ┌────────────────────────────┐ │
│ │ Worker Node │ │ Worker Node │ │
│ │ ┌──────────────┐ │ │ ┌──────────────────────┐ │ │
│ │ │ Pod │ │ │ │ Pod │ │ │
│ │ │ ┌──────────┐ │ │ │ │ ┌────────┐ ┌──────┐ │ │ │
│ │ │ │Container │ │ │ │ │ │Cont. A │ │Cont B│ │ │ │
│ │ │ └──────────┘ │ │ │ │ └────────┘ └──────┘ │ │ │
│ │ └──────────────┘ │ │ └──────────────────────┘ │ │
│ │ ┌───────┐ ┌─────┐ │ │ ┌───────┐ ┌─────┐ │ │
│ │ │kubelet│ │proxy│ │ │ │kubelet│ │proxy│ │ │
│ │ └───────┘ └─────┘ │ │ └───────┘ └─────┘ │ │
│ └────────────────────┘ └────────────────────────────┘ │
└─────────────────────────────────────────────────────────┘Authentication and authorization
The API server authenticates requests with:
- X.509 client certificates
- Service account tokens
- OpenID Connect (OIDC)
- Webhook token authentication
It then authorizes them with one or more of these modes:
| Mode | Notes |
|---|---|
| RBAC | Role-based access control, the one to use |
| ABAC | Attribute-based access control, legacy |
| Node | Authorizes kubelet requests |
| Webhook | Hands the decision to an external service |
# Example RBAC - Least privilege for deployment reader
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: deployment-reader
namespace: production
rules:
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "list"]Common misconfigurations
Docker misconfigurations
| Issue | Risk | Detection |
|---|---|---|
--privileged flag | Container escape | docker inspect |
| Docker socket mounted | Host compromise | Check container mounts |
| Running as root | Privilege escalation | docker inspect --format '{{.Config.User}}' |
| Host network mode | Network pivoting | docker inspect --format '{{.HostConfig.NetworkMode}}' |
| Sensitive mounts | Data exposure | Check /proc/mounts in container |
# Find privileged containers
docker ps -q | xargs docker inspect --format '{{.Name}} {{.HostConfig.Privileged}}'
# Find containers with socket mounted
docker ps -q | xargs docker inspect --format '{{.Name}} {{range .Mounts}}{{.Source}}{{end}}' | grep docker.sockKubernetes misconfigurations
| Issue | Risk | Detection |
|---|---|---|
| Anonymous authentication enabled | Unauthenticated API access | kubectl auth can-i --list --as=system:anonymous |
| Overly permissive RBAC | Privilege escalation | Audit ClusterRoleBindings |
| Privileged pods | Container escape | Check SecurityContext |
| Default service accounts | Token abuse | Pods without explicit SA |
| etcd unencrypted | Secret exposure | Check etcd configuration |
| Kubelet API exposed | Node compromise | Port 10250/10255 scan |
# Check for privileged pods
kubectl get pods -A -o json | jq '.items[] | select(.spec.containers[].securityContext.privileged==true) | .metadata.name'
# Check anonymous access
kubectl auth can-i --list --as=system:anonymous
# Find pods with hostPath mounts
kubectl get pods -A -o json | jq '.items[] | select(.spec.volumes[]?.hostPath != null) | {name: .metadata.name, namespace: .metadata.namespace}'Attack surface
Container escape vectors
| Vector | What it gives you |
|---|---|
| Privileged container | Full device access and kernel module loading |
| Dangerous capabilities | CAP_SYS_ADMIN or CAP_SYS_PTRACE open several escapes |
| Docker socket | Start a new privileged container with the host filesystem mounted |
| Kernel vulnerabilities | Dirty COW, OverlayFS CVEs |
| runc vulnerabilities | CVE-2019-5736 overwrites the host runc binary |
| Host path mounts | Direct writes to the host filesystem |
Kubernetes attack paths
- Compromised pod → service account token → API access
- Misconfigured RBAC → create a privileged pod → node compromise
- etcd access → extract secrets → lateral movement
- Kubelet API → exec into pods → container compromise
- Cloud metadata → IAM credentials → cloud compromise
# From compromised pod - check service account permissions
cat /var/run/secrets/kubernetes.io/serviceaccount/token | cut -d. -f2 | base64 -d
kubectl auth can-i --list
# Access cloud metadata (if not blocked)
curl http://169.254.169.254/latest/meta-data/iam/security-credentials/Security tools
Vulnerability scanning
| Tool | Purpose |
|---|---|
| Trivy | Image vulnerability scanning |
| Grype | Container image scanner |
| Kubescape | Kubernetes security scanning |
| kube-bench | CIS benchmark compliance |
| Falco | Runtime security monitoring |
# Scan image for vulnerabilities
trivy image nginx:latest
# Kubernetes security assessment
kubescape scan framework nsa
# CIS benchmark check
kube-bench run --targets=nodeOffensive tools
| Tool | Purpose |
|---|---|
| CDK | Container/Kubernetes penetration toolkit |
| Peirates | Kubernetes penetration testing |
| kubectl-who-can | RBAC enumeration |
| kube-hunter | Kubernetes vulnerability scanner |
# CDK: container escape detection
./cdk evaluate
# Peirates: Kubernetes attack automation
./peirates
# Find who can create pods
kubectl-who-can create pods -AHardening checklist
Docker
- Never run containers with
--privileged - Don't mount Docker socket in containers
- Run containers as non-root user
- Use read-only root filesystems
- Drop all capabilities, add only required
- Scan images for vulnerabilities
- Don't store secrets in images
Kubernetes
- Enable RBAC with least privilege
- Use Pod Security Standards (restricted)
- Encrypt etcd at rest
- Enable audit logging
- Use Network Policies
- Don't use default service accounts
- Block cloud metadata access
- Disable anonymous authentication
Related resources
- Docker Container Escape Techniques: escape techniques
- Container Hardening: defensive configuration
- Cloud Security: managed container services in the cloud
References
MITRE ATT&CK Techniques
- T1610 - Deploy Container - Malicious container deployment
- T1611 - Escape to Host - Container escape
- T1613 - Container and Resource Discovery - Enumeration
- T1552.007 - Container API - Credential access
- T1609 - Container Administration Command - Container manipulation
CWE entries
- CWE-250 - Execution with Unnecessary Privileges - Privileged containers
- CWE-269 - Improper Privilege Management - RBAC issues
- CWE-284 - Improper Access Control - Authentication bypass
Official documentation
Docker Container Escape Techniques
Docker container escape techniques including privileged containers, exposed Docker sockets, kernel exploits, and misconfiguration exploitation methods.
Container Hardening
Practical container security techniques covering image hardening, runtime protection, resource limits, and defensive configurations for Docker and Kubernetes environments.