Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Cloud, Container and Supply Chain Security  ›  Lesson

Hardening Containers in Docker and Kubernetes

Docker 12 minA hardened container runs as a non-root user with a read-only filesystem, no extra Linux capabilities, resource limits and only the network access it needs, enforced by Kubernetes policies.

Hardening Containers in Docker and Kubernetes

Suppose the worst happens: a bug lets an attacker run code inside the Tiffin API container. What happens next depends on settings you chose months ago. If the container runs as root with every privilege, the attacker has a strong foothold. If it is locked down, they are stuck in a small, read-only box that can talk to almost nothing.

The rule

A container may do its job and nothing else.

Start in the Dockerfile:

  • Run as a non-root user (USER 10001). Many images default to root.
  • Keep secrets out of the image; inject them at runtime.
  • Use a minimal, pinned base, as in the previous lesson.

Then enforce the rest in Kubernetes with a securityContext:

spec:
  containers:
    - name: tiffin-api
      image: registry.example/tiffin@sha256:<digest>
      securityContext:
        runAsNonRoot: true
        readOnlyRootFilesystem: true
        allowPrivilegeEscalation: false
        capabilities:
          drop: ["ALL"]
        seccompProfile:
          type: RuntimeDefault
      resources:
        limits: { cpu: "500m", memory: "256Mi" }

What each line stops:

  • runAsNonRoot: refuses to start the container as root.
  • readOnlyRootFilesystem: the attacker cannot drop tools or edit the app. Mount a small writable volume only where the app truly writes.
  • allowPrivilegeEscalation: false and dropping all capabilities: removes kernel powers that help escape the container.
  • seccompProfile: RuntimeDefault: blocks rarely needed system calls.
  • limits: a hijacked pod cannot starve the node, for example with a crypto-miner.

Two cluster-level controls finish the box. Network policies allow the API to reach only the database and the payment provider. Admission control (built-in Pod Security Admission, or a policy engine) rejects privileged pods and, ideally, any image that is not signed by Tiffin's pipeline. Never mount the container runtime socket into a pod: that hands over the whole node.

The shortcut

Imagine the attacker is already inside and ask four questions: Who am I? What can I write? What powers do I have? Whom can I reach? Every answer should be "almost nothing".

The attacker inside tiffin-apiStep 1 of 4
  1. Who am I?

    User 10001, not root. runAsNonRoot guarantees it.

Drills

Answer fast — trust the rule.

quick drill

What does readOnlyRootFilesystem: true prevent?

quick drill

Which setting is the most dangerous to allow in a Tiffin pod?

quick drill

What does a Kubernetes network policy add for the Tiffin API?

quick drill

Where should the non-root user be set?

Remember

  • A container may do its job and nothing else.
  • Inside the box ask: who am I, what can I write, what powers do I have, whom can I reach?
  • Non-root, read-only, no capabilities, limits, network policies, admission control.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainShrink what a hijacked container can do

Assume an attacker gets code execution inside the Tiffin API container. Describe the Dockerfile and Kubernetes settings you would use so that they can do as little as possible, and explain what each setting prevents.