Practices and tools for securing containers throughout their lifecycle: image building, runtime, orchestration, and compliance.
Container security is a set of practices, tools, and policies designed to protect containerized applications throughout their lifecycle. It spans from secure image building to runtime threat detection, through secure orchestration and regulatory compliance.
Unlike traditional application security, container security must consider multiple layers: the base image, dependencies, container runtime, orchestration, and underlying infrastructure. Each layer introduces unique attack vectors that require specific controls.
Container security is not an optional add-on — it's an integral discipline that must be integrated into the CI/CD pipeline from first commit to production.
Security begins in the Dockerfile. Base images should be official, minimal, and up-to-date. Distroless or Alpine Linux-based images significantly reduce attack surface by eliminating shells, package managers, and unnecessary tools.
# ❌ Insecure image
FROM ubuntu:latest
RUN apt-get update && apt-get install -y curl
COPY . /app
USER root
CMD ["node", "app.js"]
# ✅ Secure image
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
FROM gcr.io/distroless/nodejs20-debian12
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/src ./src
USER 65534
CMD ["src/app.js"]Scanning should run at multiple stages: during build, before deployment, and periodically in registries. Tools like Trivy, Grype, or Snyk identify known vulnerabilities (CVEs) in the base OS and application dependencies.
# GitHub Actions - Trivy scanning
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: 'myapp:${{ github.sha }}'
format: 'sarif'
output: 'trivy-results.sarif'
severity: 'CRITICAL,HIGH'
exit-code: '1' # Fail build if critical vulnerabilities foundImage signing with cosign ensures integrity and provenance. It implements a zero-trust model where only images signed by authorized keys can run in production.
# Sign image
cosign sign --key cosign.key myregistry/myapp:v1.0.0
# Verify in admission controller
cosign verify --key cosign.pub myregistry/myapp:v1.0.0Falco monitors kernel behavior in real-time, detecting anomalous activities like sensitive file access, privilege escalation, or suspicious network connections.
# Custom Falco rule
- rule: Unexpected Network Connection
desc: Detect unexpected outbound connections
condition: >
(spawned_process and container and
(fd.typechar = 4 and fd.net != "127.0.0.1" and not fd.net in (allowed_networks)))
output: >
Unexpected network connection (user=%user.name container=%container.name
dest=%fd.rip:%fd.rport)
priority: WARNINGKubernetes Pod Security Standards define three policy levels: Privileged, Baseline, and Restricted. The Restricted level should be the standard for production workloads.
apiVersion: v1
kind: Namespace
metadata:
name: production
labels:
pod-security.kubernetes.io/enforce: restricted
pod-security.kubernetes.io/audit: restricted
pod-security.kubernetes.io/warn: restricted
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: secure-app
spec:
template:
spec:
securityContext:
runAsNonRoot: true
runAsUser: 65534
fsGroup: 65534
seccompProfile:
type: RuntimeDefault
containers:
- name: app
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
resources:
limits:
memory: "128Mi"
cpu: "100m"Network policies implement microsegmentation, limiting communication between pods based on labels and namespaces. By default, all traffic should be denied (deny-all) with explicit exceptions.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-network-policy
spec:
podSelector:
matchLabels:
app: api
policyTypes:
- Ingress
- Egress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- protocol: TCP
port: 8080
egress:
- to:
- podSelector:
matchLabels:
app: database
ports:
- protocol: TCP
port: 5432Secrets should never be embedded in images or plain environment variables. Tools like AWS Secrets Manager, HashiCorp Vault, or Kubernetes Secrets with encryption at rest provide secure credential management.
# Secure secrets usage in Kubernetes
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- name: app
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-credentials
key: password
volumeMounts:
- name: tls-certs
mountPath: /etc/ssl/certs
readOnly: true
volumes:
- name: tls-certs
secret:
secretName: tls-certificates| Tool | Function | Strength | Limitation |
|---|---|---|---|
| Trivy | Vulnerability scanning | Fast, multiple formats | Only known vulnerabilities |
| Falco | Runtime detection | Real-time, highly configurable | Learning curve |
| OPA Gatekeeper | Admission policies | Powerful declarative language | Complexity for simple cases |
| cosign | Image signing | Native registry integration | Requires PKI infrastructure |
| Snyk | Commercial scanning | Proprietary database | Cost per developer |
Containers amplify both security benefits and risks. A vulnerable image replicates instantly to hundreds of instances. A compromised container can laterally escalate through cluster networks. Container immutability is a security advantage, but only if base images are secure from the source.
For staff+ engineering teams, container security isn't just compliance — it's architecture. Decisions about registries, admission policies, and threat detection directly impact organizational security posture. Automating security controls in the pipeline reduces developer friction while maintaining consistent standards.
The cost of remediating vulnerabilities grows exponentially: significantly more expensive in production than in development. Shift-left container security transforms security from a reactive blocker into a proactive accelerator.
Container platform that packages applications with all dependencies into portable, consistent units that run identically in any environment.
Container orchestration platform that automates deployment, scaling, and management of containerized applications at scale, becoming the de facto standard for cloud native.
Integration of security practices throughout the software development lifecycle, automating security controls in the CI/CD pipeline.
Practices for ensuring the integrity and security of all dependencies, tools, and processes comprising the software development pipeline.
Repositories for storing, versioning, and distributing container images, from public registries like Docker Hub to private registries like ECR.