Lesson 2 of 10
Container Security
Containers have transformed application deployment but introduce unique security challenges. Docker and Kubernetes misconfigurations are among the most exploited vulnerabilities in modern infrastructure.
Docker Security Hardening
# Docker security best practices + common attack vectors
# ATTACK: Running containers as root (default)
docker run ubuntu whoami # Output: root
# If attacker escapes container → root access to host
# CVE-2019-5736 (runc escape): exploiting root in container → host root
# DEFENSE: Run as non-root user
FROM node:18-alpine
# Create dedicated non-root user
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
# Copy application files
COPY --chown=appuser:appgroup . /app
WORKDIR /app
# Switch to non-root before running
USER appuser
CMD ["node", "server.js"]
# Verify: docker run myapp whoami → appuser (not root)
# Additional Docker hardening:
# 1. Read-only filesystem
docker run --read-only --tmpfs /tmp myapp
# 2. Drop Linux capabilities (principle of least privilege)
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE myapp
# Only add capabilities explicitly needed - not all 40+ Linux capabilities
# 3. Limit CPU/memory (prevent resource exhaustion)
docker run --memory=512m --cpus=1.0 myapp
# 4. Scan image for vulnerabilities
trivy image myapp:latest # Trivy scans OS packages + language libs
docker scan myapp:latest # Docker Scout
# Block deployment if CRITICAL vulnerabilities found in CI/CD
# 5. Distroless or minimal base images
FROM gcr.io/distroless/python3-debian11 # No shell, no package manager
# Reduces attack surface: no bash, no curl, no wget for attacker to useKubernetes Security
# Kubernetes security hardening
# 1. RBAC - Kubernetes Role-Based Access Control
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: production
name: app-reader
rules:
- apiGroups: [""]
resources: ["pods", "services"]
verbs: ["get", "list", "watch"] # Read-only - not create, delete, update
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: app-reader-binding
namespace: production
subjects:
- kind: ServiceAccount
name: my-app-sa
roleRef:
kind: Role
name: app-reader
apiGroup: rbac.authorization.k8s.io
# 2. Pod Security Standards
apiVersion: v1
kind: Pod
spec:
securityContext:
runAsNonRoot: true # Enforce non-root
runAsUser: 1000
seccompProfile:
type: RuntimeDefault # Apply default seccomp profile (syscall filtering)
containers:
- name: app
securityContext:
allowPrivilegeEscalation: false # Cannot gain more privileges
readOnlyRootFilesystem: true # Immutable container filesystem
capabilities:
drop: ["ALL"] # Drop all capabilities
# 3. Network Policies (micro-segmentation)
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: app-isolation
spec:
podSelector:
matchLabels:
app: backend
ingress:
- from:
- podSelector:
matchLabels:
app: frontend # Only frontend can reach backend
ports:
- port: 8080Common Mistakes
- Exposing Docker socket to containers - mounting /var/run/docker.sock gives a container full control of the host Docker daemon and every other container; equivalent to root on the host
- Using :latest tags for base images in production - latest changes; pin to specific digest (FROM ubuntu@sha256:...) to ensure reproducible, auditable builds
- No NetworkPolicy in Kubernetes - without NetworkPolicy, all pods can communicate with all other pods in the cluster; lateral movement between services is unrestricted
Tip
Tip
Practice Container Security in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Container Security
Practice Task
Note
Practice Task - (1) Write a working example of Container Security from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Container Security is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cybersecurity code.
Key Takeaways
- Containers have transformed application deployment but introduce unique security challenges.
- Exposing Docker socket to containers - mounting /var/run/docker.sock gives a container full control of the host Docker daemon and every other container; equivalent to root on the host
- Using :latest tags for base images in production - latest changes; pin to specific digest (FROM ubuntu@sha256:...) to ensure reproducible, auditable builds
- No NetworkPolicy in Kubernetes - without NetworkPolicy, all pods can communicate with all other pods in the cluster; lateral movement between services is unrestricted
Finished this lesson?