Containers have become the standard way to package applications. Each image bundles an operating system userland, runtime, libraries, application code and configuration. That makes images convenient to deploy, and it means every image carries its own attack surface.
This guide focuses on what application teams control: the Dockerfile, the image pipeline and the Kubernetes manifests they ship.
Build secure images
Start small
Every package in an image is something to patch. Use minimal base images:
- distroless images contain only the application runtime, with no shell or package manager,
- Alpine or slim variants of official images, and
- a scratch base for static binaries, such as Go.
Use multi-stage builds
Keep compilers and build tools out of the runtime image:
# Build stage
FROM golang:1.18 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/api ./cmd/api
# Runtime stage: no shell, no package manager, non-root user
FROM gcr.io/distroless/static:nonroot
COPY --from=build /out/api /api
USER nonroot:nonroot
EXPOSE 8080
ENTRYPOINT ["/api"]Run as non-root
Containers running as root make container escapes and host compromise more likely. Set a non-root USER in the image, and enforce it in Kubernetes.
Pin versions
Pin base images by digest (@sha256:…), not floating tags like latest, so builds are reproducible and reviewable. Update pins deliberately, with automated pull requests.
Keep secrets out
Never COPY credentials or .env files into images, and do not pass secrets as build arguments; they persist in image layers. Use a .dockerignore file, and inject secrets at runtime.
Scan images, twice
Scan in CI to catch known vulnerabilities before release, and scan continuously in the registry, because new CVEs are published after you build:
trivy image --severity HIGH,CRITICAL --ignore-unfixed --exit-code 1 \
registry.example.com/orders-api:2.7.0Prioritise fixable vulnerabilities in packages that are actually used at runtime, and rebuild images regularly to pick up base image patches.
Sign images and verify them
Signing proves an image was built by your pipeline and has not been altered. Sigstore's cosign, which reached version 1.0 last year, makes this straightforward:
cosign sign --key cosign.key registry.example.com/orders-api@sha256:1f0c...
cosign verify --key cosign.pub registry.example.com/orders-api@sha256:1f0c...At deployment, an admission controller can verify signatures and reject unsigned images.
Kubernetes manifests: what application teams own
apiVersion: apps/v1
kind: Deployment
metadata:
name: orders-api
spec:
replicas: 3
selector: { matchLabels: { app: orders-api } }
template:
metadata:
labels: { app: orders-api }
spec:
automountServiceAccountToken: false
securityContext:
runAsNonRoot: true
seccompProfile: { type: RuntimeDefault }
containers:
- name: api
image: registry.example.com/orders-api@sha256:1f0c...
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities: { drop: ["ALL"] }
resources:
requests: { cpu: 100m, memory: 128Mi }
limits: { memory: 256Mi }- Drop privileges: non-root, no privilege escalation, drop all Linux capabilities, read-only root file system.
- Do not mount service account tokens unless the application calls the Kubernetes API.
- Set resource requests and limits to contain runaway or abused workloads.
- Restrict network traffic with NetworkPolicies, allowing only the connections the service needs.
- Use Secrets or an external secrets manager for credentials, never plain environment variables in manifests committed to Git.
Policy enforcement is changing
Kubernetes is replacing PodSecurityPolicy, deprecated since 1.21, with Pod Security Admission, which has been beta since 1.23 and will become the built-in mechanism when PSP is removed in 1.25. It enforces the Pod Security Standards (privileged, baseline, restricted) per namespace. Manifests written to the restricted profile, like the one above, will keep working. Policy engines such as Kyverno and OPA Gatekeeper add rules like "only signed images from our registry".
Runtime
Preventive controls reduce risk; runtime detection catches what gets through. Tools such as Falco alert on suspicious behaviour inside containers, such as a shell being spawned, unexpected network connections or writes to sensitive paths.
The bottom line
Most container security is decided before deployment: small non-root images, pinned and scanned dependencies, signatures, and manifests that drop privileges. Application teams own those decisions. Get them right, and the platform's policies and runtime detection become a safety net rather than the only line of defence.
Our container security and Kubernetes security review images, pipelines and manifests against these practices.