Sub topic 1
What Containers Protect Against, and What They Do Not
Prerequisites
You should know how to build and run Docker images, write a basic Dockerfile, use the Linux command line, and understand users, permissions, processes, and registries.
The Container Threat Model
A container packages an application and its user-space dependencies. It limits accidental access between applications and makes deployments more consistent. A compromised application usually cannot directly read another container's files or processes.
A container is not a virtual machine. Containers share the host's Linux kernel. A kernel vulnerability, an exposed Docker socket, excessive privileges, or a misconfigured volume can let an attacker reach beyond the container. Treat the host, Docker daemon, images, and runtime settings as part of the security boundary.
How Isolation Works
Docker relies on Linux kernel features:
- Namespaces hide resources. A process namespace hides other processes, a network namespace provides separate interfaces, and a mount namespace provides a separate filesystem view.
- Cgroups limit and measure resource use such as memory, CPU, and process count. They reduce denial-of-service risk from one container consuming all host resources.
- Capabilities split root's powers into smaller permissions. Dropping unused capabilities limits what a process can do even if it runs as root inside the container.
These controls reduce the impact of a compromised process, but they do not make unsafe code harmless. Use a non-root user and apply runtime restrictions as additional layers.
Choosing and Pinning Base Images
Choose images from trusted publishers and keep them small. Fewer packages mean fewer vulnerabilities and less code to maintain. An image tag such as python:3.12-slim is readable, but its contents can change. Pin a version, and use a digest when you need a specific immutable image.
Multi-Stage Builds
Multi-stage builds keep compilers and package managers out of the runtime image. The first stage builds the application. The final stage copies only the files required to run it.
# syntax=docker/dockerfile:1
FROM golang:1.22 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app ./cmd/app
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/app /app
USER nonroot:nonroot
ENTRYPOINT ["/app"]Exercise
Why does a multi-stage build improve security?
Show answer
It leaves build tools and their dependencies out of the final image, reducing its attack surface.