liveBook · Manning

Init container

As explained in Road to Kubernetes

An init container is a Kubernetes container that runs to completion before the main containers in a Pod start.

An init container is a Kubernetes container that runs to completion before the main containers in a Pod start. It prepares conditions the application needs without remaining active afterward. section 8.4.2

Preparing a PostgreSQL volume

A StatefulSet mounts a new volume containing lost+found. An Alpine init container mounts the same volume, runs sh -c "rm -rf /mnt/lost+found", and exits before PostgreSQL starts. section 8.4.2

Listing 8.19 StatefulSet for Postgres, part 3
...
spec:
  ...
  template:
    metadata:
      labels:
        app: postgres
    spec:
      initContainers:
        - name: delete-lost-found
          image: alpine:latest
          command: ["sh", "-c", "rm -rf /mnt/lost+found"]
          volumeMounts:
            - name: db-vol
              mountPath: /mnt
  volumeClaimTemplates:
  - metadata:
      name: db-vol
    spec:
      ...

Inspect the StatefulSet configuration showing the init container, Alpine image, cleanup command, and shared volume mount. section 8.4.2

How Road to Kubernetes covers it

How deep it goes
Defines execution order and demonstrates a cleanup command, but does not develop broader initialization patterns.
Examples
Uses an Alpine container to remove lost+found from a PostgreSQL volume before startup.
What you can do after
Configure a StatefulSet that cleans a mounted volume before its PostgreSQL container runs.
How it connects
Connects init containers with StatefulSets, Pods, persistent volumes, and PostgreSQL.

Also explained in

Compare how 2 books explain init container →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Road to Kubernetes. The section links point to the parts of the book it draws on.