Lesson 3 / 10
Module 3Lesson 1⏱ 50 min study

Lesson 3: Automating Container Runtimes & Image Pipelines

Automate Docker and Podman host configurations, design lean multi-stage build pipelines, and apply defence-in-depth container security hardening.

✦ Key Architectural Takeaways
  • •Container runtimes must be configured with systemd cgroup drivers to prevent resource conflicts with Kubernetes kubelet.
  • •Multi-stage Dockerfiles isolate heavy build toolchains from lean production images, slashing vulnerability surface and image transfer times.
  • •Containers should never run as root; unprivileged UIDs, read-only root filesystems, and dropped capabilities mitigate escape vulnerabilities.
  • •Ansible's community.docker and containers.podman collections provide declarative management of container images, networks, and volumes.
  • •Health checks and restart policies must be declared explicitly to ensure automatic recovery from runtime faults.

1. Automating Container Engine Installation

Kubernetes requires an OCI-compliant Container Runtime Interface (CRI). For standalone development or host-level auxiliary tooling, engines like Docker and Podman are automated using Ansible.

A critical requirement when configuring container engines for Kubernetes is ensuring the runtime utilizes the `systemd` cgroup driver rather than the legacy cgroupfs driver. Having two distinct cgroup managers causes instability when system memory pressure occurs.

Ansible Playbook: Hardened Docker Daemon Configuration

YAMLSYSTEM SCHEMATIC
---
- name: Deploy and Configure Docker Engine with Systemd Cgroup Driver
  hosts: container_hosts
  become: true
  tasks:
    - name: Ensure required dependencies are present
      ansible.builtin.apt:
        name:
          - ca-certificates
          - curl
          - gnupg
        state: present
        update_cache: true

    - name: Configure Docker daemon JSON with security limits
      ansible.builtin.copy:
        dest: /etc/docker/daemon.json
        owner: root
        group: root
        mode: "0644"
        content: |
          {
            "exec-opts": ["native.cgroupdriver=systemd"],
            "log-driver": "json-file",
            "log-opts": {
              "max-size": "50m",
              "max-file": "3"
            },
            "storage-driver": "overlay2",
            "live-restore": true,
            "no-new-privileges": true
          }
      notify: Restart Docker Daemon

  handlers:
    - name: Restart Docker Daemon
      ansible.builtin.systemd:
        name: docker
        state: restarted
§

2. Multi-Stage Dockerfile Architecture

A major vulnerability in containerized systems is including compilers, package managers, and debug tools in production container images.

The Anti-Pattern: Single-Stage Build

DOCKERFILESYSTEM SCHEMATIC
# INSECURE & BLOATED (Size: ~1.2 GB)
FROM golang:1.22
WORKDIR /app
COPY . .
RUN go build -o server .
EXPOSE 8080
CMD ["./server"]
# Contains full Go compiler, git, curl, bash, and build toolchains!

The Production Pattern: Multi-Stage Build

DOCKERFILESYSTEM SCHEMATIC
# STAGE 1: Compilation Sandbox
FROM golang:1.22-alpine AS builder
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o /bin/server .

# STAGE 2: Distroless Minimal Runtime (Size: ~18 MB)
FROM gcr.io/distroless/static-debian12:nonroot
WORKDIR /app
COPY --from=builder /bin/server /app/server
USER nonroot:nonroot
EXPOSE 8080
ENTRYPOINT ["/app/server"]
# Contains ZERO shell, zero package manager, and runs as UID 65532!
§

3. Container Hardening Checklist

When deploying containerized workloads, enforce defense-in-depth security:

ARCHITECTURE DIAGRAMSYSTEM SCHEMATIC
+----------------------------------------------------------------+
|                   CONTAINER ISOLATION BOUNDARY                 |
|                                                                |
|  +----------------------------------------------------------+  |
|  | User Isolation: Runs as unprivileged UID 10001           |  |
|  | (Root execution disabled via securityContext)            |  |
|  +----------------------------------------------------------+  |
|  | Filesystem: Read-Only Root Filesystem (readOnlyRootFilesystem|
|  | (Writable paths restricted to isolated in-memory tmpfs)  |  |
|  +----------------------------------------------------------+  |
|  | Linux Capabilities: Drop ALL ('CAP_DROP: ALL')           |  |
|  | (Prevent raw sockets, kernel probing, and privilege escalation
|  +----------------------------------------------------------+  |
|  | Syscall Filtering: Enforce seccomp=RuntimeDefault        |  |
+--+----------------------------------------------------------+--+
  1. 1Non-Root Execution: Configure USER 10001:10001 in Dockerfiles or runAsNonRoot: true in Kubernetes security contexts.
  2. 2Read-Only Root Filesystem: Prohibits attackers from writing scripts or modifying binaries if an application vulnerability is exploited.
  3. 3Drop All Capabilities: Linux divides root privileges into distinct capabilities (e.g., CAP_CHOWN, CAP_NET_RAW, CAP_SYS_ADMIN). Drop all capabilities by default, adding back only what is strictly necessary.
  4. 4No New Privileges: Prevent child processes from acquiring additional privileges via setuid binaries.
§

4. Orchestrating Containers with Ansible

Ansible's community.docker collection enables declarative deployment of local containers:

YAMLSYSTEM SCHEMATIC
- name: Deploy Secure Isolated NGINX Reverse Proxy
  community.docker.docker_container:
    name: edge-proxy
    image: nginx:1.25-alpine
    state: started
    restart_policy: always
    read_only: true
    user: "101:101"
    capabilities:
      - NET_BIND_SERVICE
    cap_drop:
      - ALL
    ports:
      - "80:8080"
    tmpfs:
      - /var/cache/nginx:rw,noexec,nosuid,size=50m
      - /var/run:rw,noexec,nosuid,size=10m