Init Containers

Difficulty: Intermediate

Question

What is an init container in Kubernetes, how does it differ from a regular container, and when would you use one?

Answer

An init container is a container in a Pod that runs to completion before any of the app containers start. If a Pod has several init containers they run one at a time, in order, and each must exit with status 0 before the next begins. They are used for setup work such as waiting for a dependency, running a migration, fetching config, or fixing file permissions on a shared volume.

How the kubelet runs them

  1. The Pod is scheduled and its volumes are mounted.
  2. Init container 1 starts. The kubelet waits for it to exit successfully.
  3. Init container 2 starts, and so on through the list.
  4. Only after all of them succeed do the regular containers in spec.containers start, in parallel.

If an init container fails, the kubelet restarts it according to the Pod's restartPolicy. With Always or OnFailure it keeps retrying with backoff, and the Pod shows Init:CrashLoopBackOff. With restartPolicy: Never, a failed init container marks the whole Pod as failed. While they run, kubectl get pods shows status like Init:0/2.

How they differ from app containers

AspectInit containerApp container
When it runsBefore app containers, sequentiallyAfter all init containers, in parallel
LifetimeMust exit 0Long running
ProbesNo liveness, readiness or startup probesSupports all probes
Resource accountingPod's effective init request is the highest single init requestRequests are summed
ImageCan use a different image with tools you do not want in the app imageThe app image

The resource rule matters for scheduling: the Pod's effective request is the larger of the sum of app container requests and the largest init container request. A heavy one-off init step can therefore make a Pod harder to schedule.

Typical real-world uses

  • Wait for a dependency: loop until postgres.db resolves or the port opens, so the app does not crash on boot.
  • Database migrations: run Flyway, Liquibase or prisma migrate deploy before the API starts. Make them idempotent, since every replica will run the init container.
  • Fetch secrets or config: pull files from Vault or S3 into an emptyDir that the app then reads.
  • Prepare the filesystem: chown a volume, clone a git repo, or copy static assets.
  • Security separation: tools like curl or git live in the init image, so the production image stays minimal.

Native sidecars are also declared as init containers

Since Kubernetes 1.29 (enabled by default, and stable in 1.33), an init container with restartPolicy: Always becomes a native sidecar. It starts in init order but keeps running alongside the app, supports probes, and is shut down after the app containers. This fixed long-standing problems with log shippers and service mesh proxies. An interviewer may ask about it, so be clear that a plain init container still runs to completion, while one with restartPolicy: Always is a sidecar.

Gotchas

  • Init containers run again if the Pod is recreated, and can run more than once if restarted, so their work must be idempotent.
  • A "wait for DB" loop with no timeout can leave Pods stuck in Init:0/1 forever, hiding the real problem. Readiness probes and app-level retries are often the better fix.
  • Logs are per container: use kubectl logs <pod> -c <init-container-name> to see why init failed.
Pod startup with init containers. Each init container must exit successfully, in order, before the app containers start.
  1. Pod scheduled, volumes mounted: Shared volumes like emptyDir are ready for all containers.
  2. Init container 1 runs: For example, wait until the database port is reachable.
  3. Init container 2 runs: For example, run idempotent schema migrations.
  4. Failure triggers retry with backoff: The Pod shows Init:CrashLoopBackOff until it succeeds.
  5. App containers start together: Probes begin and the Pod can become Ready.

Code examples

Wait for Postgres, run migrations, then start the API

apiVersion: v1
kind: Pod
metadata:
  name: payments-api
spec:
  initContainers:
    - name: wait-for-db
      image: busybox:1.36
      command: ["sh", "-c", "until nc -z postgres.db 5432; do echo waiting; sleep 2; done"]
    - name: migrate
      image: registry.example.com/payments-migrations:1.4.0
      command: ["./migrate", "up"]          # must be idempotent
      envFrom:
        - secretRef:
            name: payments-db
  containers:
    - name: api
      image: registry.example.com/payments-api:1.4.0
      ports:
        - containerPort: 8080

The API container never starts until Postgres is reachable and migrations have succeeded. If the migration fails, the Pod stays in an Init state instead of serving traffic against an outdated schema.

Debugging a stuck init container

kubectl get pod payments-api            # STATUS: Init:1/2 or Init:CrashLoopBackOff
kubectl describe pod payments-api       # events and per-init-container state
kubectl logs payments-api -c migrate    # logs from the failing init container

Always pass `-c` with the init container name, since `kubectl logs` defaults to an app container.

Key points

  • Init containers run sequentially and each must exit 0 before the next one, and before app containers start.
  • Failure behaviour follows the Pod's restartPolicy; `Never` fails the Pod.
  • They do not support probes and their work should be idempotent.
  • Effective Pod request = max(sum of app requests, largest init request).
  • An init container with `restartPolicy: Always` is a native sidecar (stable in Kubernetes 1.33).

Common follow-up questions

  • How would you implement a sidecar before native sidecar support existed, and what problems did it have?
  • Should database migrations run in an init container or a separate Job? Why?
  • How are resource requests calculated for a Pod with init containers?
  • What happens if an init container keeps failing in a Deployment?

Interview tips

  • Give a concrete use case like wait-for-db or migrations instead of a generic definition.
  • Mention idempotency; it shows you have thought about multiple replicas running the same init step.
  • Bringing up native sidecars (`restartPolicy: Always`) signals that your knowledge is current.

Concepts covered

Init Containers, Pod Lifecycle, Native Sidecars, restartPolicy, Shared Volumes

More Kubernetes & Orchestration questions