LESSON 60 · ADVANCED5 min read

Containers, Kubernetes, and Supply-Chain Security

Harden container images and runtimes, secure Kubernetes access, and build provenance into the software supply chain.

OWASP Top 10 · MITRE ATT&CK · NIST CSF 2.0Free to readModule 12 of 14
YOUR NEXT SMALL WINS

By the end, you’ll be able to…

  • Explain container isolation and the risks of privileged or bloated images.
  • Harden a Dockerfile and a Kubernetes workload manifest.
  • Apply least privilege to Kubernetes service accounts and network policies.
  • Describe how SBOMs, signing, and provenance strengthen software supply-chain trust.

01A Container Is a Process With Boundaries

A container packages an application and its dependencies into an image. At runtime, it is a process isolated with Linux namespaces and restricted with cgroups and security features such as seccomp, AppArmor, or SELinux. Containers share the host kernel, so a kernel vulnerability or a privileged container can weaken isolation. Containers are not virtual machines, and they are not a security boundary by themselves.

Image content matters. A large base image contains more packages, more vulnerabilities, and more tools an attacker can use after compromise. Use a minimal, maintained base image. Pin versions by digest where possible. Rebuild regularly to pick up patches. Do not bake secrets into layers; anyone who can pull the image can read them. Multi-stage builds keep compilers and build tools out of the runtime image.

Run as a non-root user, make the filesystem read-only where possible, drop Linux capabilities, and set resource limits. Do not mount the Docker socket into a container. Do not use privileged mode unless you have a specific, documented reason and a compensating control. Treat a container compromise as a host compromise until you can prove otherwise.

DockerfileDockerfile
FROM node:22-alpine AS buildWORKDIR /appCOPY package*.json ./RUN npm ci --omit=devCOPY . .RUN npm run buildFROM node:22-alpineRUN addgroup -S app && adduser -S app -G appWORKDIR /appCOPY --from=build --chown=app:app /app/dist ./distCOPY --from=build --chown=app:app /app/node_modules ./node_modulesCOPY --from=build --chown=app:app /app/package.json ./USER appEXPOSE 3000CMD ["node", "dist/server.js"]
Outputstdout
Build stage contains compilers and dev dependencies.
Runtime stage runs as non-root app user with production files only.

02Kubernetes: Access, Isolation, and Admission

Kubernetes adds a control plane, an API server, and many identities. Every pod runs with a service account unless configured otherwise. A pod that can talk to the API server with a powerful service account can create, modify, or delete workloads. Disable automatic service-account token mounting where it is not needed, use dedicated service accounts, and bind only the RBAC permissions the workload requires.

Namespaces are not a security boundary by themselves. Use network policies to restrict pod-to-pod traffic, especially to databases and the API server. Apply pod security standards to prevent privileged containers, host networking, host path mounts, and other risky settings. Use admission controllers to enforce policy at creation time. Audit the API server and monitor for unusual role bindings, secret access, and exec into pods.

Secrets in Kubernetes are base64-encoded by default, not encrypted at rest unless the cluster is configured for it. Restrict who can read secrets, enable encryption at rest, and prefer external secret managers or workload identity for cloud credentials. Rotate secrets and audit access. A secret that is available to every pod in a namespace is not a secret for long.

hardened-deployment.yamlYAML
apiVersion: apps/v1kind: Deploymentmetadata:  name: apispec:  replicas: 2  selector:    matchLabels:      app: api  template:    metadata:      labels:        app: api    spec:      automountServiceAccountToken: false      securityContext:        runAsNonRoot: true        runAsUser: 10001        fsGroup: 10001        seccompProfile:          type: RuntimeDefault      containers:        - name: api          image: registry.example.com/api@sha256:abc123          ports:            - containerPort: 3000          securityContext:            allowPrivilegeEscalation: false            readOnlyRootFilesystem: true            capabilities:              drop: ["ALL"]          resources:            limits:              cpu: "500m"              memory: "256Mi"
Outputstdout
Pod runs as non-root, drops all capabilities,
has a read-only root filesystem, and cannot mount a service token.

03The Software Supply Chain Is Part of Your Attack Surface

Modern applications are mostly other people's code. A build pulls base images, package dependencies, GitHub Actions, and internal artifacts. Each link is a trust decision. A compromised dependency, a malicious package update, or a stolen maintainer token can put attacker code inside your signed application.

Generate a software bill of materials (SBOM) so you know which components are present. Scan dependencies and images for known vulnerabilities, but do not stop at the scanner. Pin dependencies, review lockfile changes, use private registries or allowlists where practical, and monitor advisories for the components you actually use. Remove unused dependencies; every package is code you may have to trust.

Sign artifacts and record provenance. A signature lets consumers verify who built an artifact and that it has not changed. Provenance records how it was built, from which source commit, with which builder. Together they make tampering detectable. Protect CI/CD secrets, require review for workflow changes, and treat a pull request that modifies the build pipeline as a high-risk change. The build system is production infrastructure.

supply-chain-checks.shShell
# Inventory dependencies and lockfiles.npm ci --ignore-scriptsnpm audit --omit=dev || true# Generate an SBOM with a maintained tool.syft dir:. -o spdx-json > sbom.spdx.json# Scan an image for known vulnerabilities.trivy image --severity HIGH,CRITICAL registry.example.com/api:latest# Verify a signed artifact and its provenance in a lab.cosign verify registry.example.com/api:latestcosign verify-attestation --type slsaprovenance registry.example.com/api:latest
Outputstdout
SBOM written: sbom.spdx.json
HIGH: 1  CRITICAL: 0
Verified: artifact signature and provenance match the expected builder.

04Build a Pipeline You Can Defend

A secure pipeline has short-lived identities, least-privilege deployment roles, protected branches, required reviews, and isolated environments. Secrets should be fetched at runtime from a secret manager, not stored as plaintext CI variables. Build logs should not print tokens or customer data. Artifacts should be immutable and traceable to a commit.

Add checks at the right stages. Pre-commit hooks catch obvious secrets. Pull-request checks run static analysis, dependency review, and unit tests. Build stages produce signed artifacts and SBOMs. Deployment stages verify signatures and policy before applying changes. Runtime stages monitor for drift, unexpected network connections, and privilege escalation.

No single check is sufficient. The goal is a chain of evidence: this artifact came from this commit, was built by this pipeline, passed these checks, and was deployed by this identity. When an incident happens, that chain shortens the investigation and limits the blast radius. When it is missing, every deployment becomes an unverifiable guess.

LET IT CLICK

The ideas worth keeping.

  • Containers share the host kernel; privileged containers and host mounts break isolation.
  • Use minimal images, non-root users, dropped capabilities, and read-only filesystems.
  • Kubernetes access is identity-driven; scope service accounts, use network policies, and enforce admission controls.
  • SBOMs, signatures, and provenance make the software supply chain verifiable.
TURN UNDERSTANDING INTO SOMETHING YOU CAN DO

Make it yours.

Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.

01

Exercise 1: Harden a Dockerfile

A Dockerfile starts FROM node:latest, runs as root, copies .env into the image, installs dev dependencies, and uses the default filesystem. List five changes you would make and why.

Explore a worked solution
one_possible_solution.mdCybersecurity
1. Use a pinned, minimal base image such as node:22-alpine or a distroless image. 2. Create and switch to a non-root user. 3. Remove .env and other secrets; inject them at runtime from a secret manager. 4. Use a multi-stage build and install only production dependencies in the runtime stage. 5. Set a read-only root filesystem at runtime and add resource limits. Also pin dependencies with a lockfile and scan the image for vulnerabilities.

Each change reduces attack surface or limits the impact of a compromised process. A smaller image and a non-root user are high-value defaults.

02

Exercise 2: Review a Kubernetes manifest

A pod runs as root, mounts the host path /var/run/docker.sock, has no resource limits, and automounts a service account token. Explain the risks and propose fixes.

Explore a worked solution
one_possible_solution.mdCybersecurity
Running as root increases the impact of a container escape. Mounting the Docker socket gives the container control over the host's container runtime, which is effectively host-level compromise. No resource limits allows a denial-of-service against the node. Automounting a token gives the pod API credentials it may not need. Fixes: run as a non-root user, remove the Docker socket mount, set CPU and memory limits, set automountServiceAccountToken: false unless required, drop capabilities, use a read-only root filesystem, and enforce the policy with an admission controller.

The Docker socket and host path mounts are especially dangerous because they break the intended isolation boundary.

A SMALL QUESTION. A USEFUL NEXT STEP.

What stayed with you?

Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.

A LITTLE ACTIVE RECALL0 / 4 answered
01What provides container isolation on Linux?
02Why is mounting the Docker socket into a container dangerous?
03What is an SBOM?
04Why should build pipelines be treated as production infrastructure?

Keep exploring.

The official references behind this lesson, when you want a second angle or the full details.

One lesson closer.

Reading is a start. Trying it yourself makes the connection.

Completion is saved in this browser. It records reading and hands-on practice progress.