CKAD · 5 November 2026 · 9 min read

Init containers in Kubernetes: how they work, with examples

Init containers run one at a time, in order, and must finish before your app starts. How they work, worked YAML examples, how they differ from sidecars and from Docker, and how to debug a Pod stuck in Init.

An init container is a container that runs and finishes before the application containers in a Kubernetes Pod start. Use one to prepare something the app needs: wait for a database, download a config file, run a migration, or fix permissions on a volume. The app does not start until every init container has succeeded.

TL;DR

  • Init containers live in spec.initContainers, run one at a time in order, and must exit 0.
  • The app containers start only after every init container has succeeded.
  • A failing init container shows Init:Error, Init:CrashLoopBackOff or Init:ImagePullBackOff; read it with kubectl logs <pod> -c <init-container>.
  • Since Kubernetes 1.35 an init container can set its own restartPolicy: Never to fail the Pod at once instead of retrying.
  • Add restartPolicy: Always and it becomes a sidecar that keeps running instead.
  • docker run --init is not an init container; the Compose equivalent is depends_on with service_completed_successfully.
  • Exam: CKAD Application Design and Build (20%); Pods stuck in Init are CKA Troubleshooting (30%).

Init containers are part of the CKAD curriculum under multi-container Pod design, and they are a common reason for Pods that never reach Running. This article is part 2 of the Kubernetes Pod Patterns series. It explains the problem init containers solve, how they behave, works through an example, compares them with sidecars and with Docker, and shows how to debug a Pod stuck in Init.

The problem init containers solve

Many apps assume the world is ready when they start: the database answers, the config file exists, the volume has the right owner. In a cluster, Pods start in any order, so an app that starts before its database must retry or crash and be restarted, and the tools to check end up in the app image.

An init container moves that setup out of the app. The Kubernetes docs give the reasons: an init container can carry tools such as sed, awk, python or dig that the app image doesn't have; it can be given access to Secrets the app containers can't see; it can block the app until a set of preconditions are met; and the app image stays smaller, with less to attack.

Wait for the database before the app starts. Left, in amber, without an init container: the app starts at once, the database isn't reachable yet, the app must retry or it crashes and restarts, and any checking tools go in the app image. An arrow leads right, in green, to with an init container: wait-for-db loops until db resolves, the Pod waits at Init:0/1, the app starts once with its database there, and nslookup stays in the init image. A caption says the app starts only when its preconditions hold.

How init containers work

Init containers are listed under spec.initContainers. They differ from ordinary containers in three ways:

  • They run one at a time, in the order listed. The second does not start until the first has finished.
  • Each must run to completion and exit with status 0. A long-running process here blocks the Pod forever.
  • The application containers in spec.containers do not start until every init container has succeeded.

The kubelet starts the first init container once the Pod's networking and storage are ready, so an init container can use the network and the Pod's volumes. While they run, the Pod is Pending with its Initialized condition false, and it can't become Ready.

Init containers run in order, then the app starts, drawn as a timeline. Init 1, wait-for-db, first exits 1 and is retried, then runs and exits 0, marked with a green tick. Only then does init 2, write-page, run and exit 0, with a tick. Only then does the app, nginx, start and keep running. Under the time axis, the Pod's STATUS reads Init:0/2 during init 1, Init:1/2 during init 2, and PodInitializing then Running when the app starts.

If an init container fails, the kubelet retries it according to the Pod's restartPolicy. With the default Always, or with OnFailure, it retries with an increasing back-off. With Never, the whole Pod is marked as failed. From Kubernetes 1.35 an init container can override this with its own restartPolicy, for example Never to fail the Pod at once if setup fails (shown below).

Init containers share the Pod's volumes and network with the app but can use a different image, and their names must be unique among all the Pod's containers.

Init containers in a real cluster

Init containers in a real cluster. Readers send HTTP to Service web, which sends port 80 traffic to the nginx app container in Pod web on worker node 1. In the same Pod, init 1, wait-for-db, asks the cluster DNS until the name db resolves to Service db, which fronts Pod db, the database, on worker node 2. Init 2, write-page, writes index.html into an emptyDir volume named html, and nginx, which starts last, serves it.

The init container never talks to the database Pod directly: it waits until the cluster DNS resolves the Service name db. The second writes a file into a volume, and nginx serves it once it starts. Each replica of a Deployment runs its own init containers, every time a new Pod is created, so write them to be safe to repeat: the docs' own advice is that init container code "should be idempotent".

Example: wait for a Service, then write a file

This Pod is the one in the picture. The first init container waits until a Service called db resolves in DNS. The second writes a page into a shared emptyDir volume. Only then does nginx start and serve that page.

YAML
apiVersion: v1kind: Podmetadata:  name: webspec:  initContainers:  - name: wait-for-db    image: busybox:1.36    command: ['sh', '-c', 'until nslookup db.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for db; sleep 2; done']  - name: write-page    image: busybox:1.36    command: ['sh', '-c', 'echo "<h1>Ready at $(date)</h1>" > /work/index.html']    volumeMounts:    - name: html      mountPath: /work  containers:  - name: nginx    image: nginx:1.27    volumeMounts:    - name: html      mountPath: /usr/share/nginx/html  volumes:  - name: html    emptyDir: {}

Apply it in a namespace with no db Service and watch:

Shell
kubectl apply -f web.yamlkubectl get pod web -w

The status stays at Init:0/2, because the first init container is looping until the name db resolves. Create the Service in another terminal:

Shell
kubectl create service clusterip db --tcp=5432:5432

Within a few seconds the status moves through Init:1/2 and PodInitializing to Running. Check the page the init container wrote:

Shell
kubectl exec web -c nginx -- cat /usr/share/nginx/html/index.html

Fail fast instead of retrying (Kubernetes 1.35 and later)

Container restart rules, on by default since 1.35, let a regular init container set its own restartPolicy. With Never, a failed setup step fails the Pod straight away, even in a Pod whose own policy is Always:

Shell
  initContainers:  - name: check-config    image: busybox:1.36    restartPolicy: Never        # fail the Pod if this fails; no retries    command: ['sh', '-c', 'test -s /config/app.yaml']    volumeMounts:    - name: config      mountPath: /config

Only Always turns an init container into a sidecar; Never and OnFailure keep it a run-once init container.

Init container vs sidecar

A sidecar is a helper that runs alongside the app for the whole life of the Pod, such as a log shipper or a proxy. Native sidecars (on by default since Kubernetes 1.29, stable since 1.33) are declared as init containers with one extra field, restartPolicy: Always:

Shell
  initContainers:  - name: log-shipper    image: busybox:1.36    restartPolicy: Always       # this makes it a sidecar, not a run-once init container    command: ['sh', '-c', 'tail -F /var/log/app/app.log']
  • Init container (no restartPolicy, or Never or OnFailure): runs once, must exit 0, blocks the next container until it does.
  • Sidecar (restartPolicy: Always, the only value that makes a sidecar): starts, keeps running beside the app, is restarted if it exits, and stops after the app.

Sidecars take their place in the init container order: an init container listed after a sidecar starts once the sidecar is running. Sidecars have their own guide: Kubernetes sidecar containers.

Init containers and Docker

People searching for a "Docker init container" usually want one of two things, and Docker has neither built in:

  • docker run --init is not an init container. It runs a tiny init process (tini) as PID 1 inside your container, so signals are forwarded and zombie processes are reaped. It does not run a separate setup step before your app.
  • Run-once setup before the app is closest to Docker Compose's depends_on with condition: service_completed_successfully. The setup service runs and exits, and Compose starts the app only if it exited with status 0.
Shell
services:  setup:    image: busybox:1.36    command: ['sh', '-c', 'echo ready > /work/index.html']    volumes: ['html:/work']  web:    image: nginx:1.27    volumes: ['html:/usr/share/nginx/html']    depends_on:      setup:        condition: service_completed_successfullyvolumes:  html: {}

Kubernetes init containers do the same job inside one Pod, with ordering, retries and a status (Init:N/M) you can watch.

Debugging a Pod stuck in Init

The STATUS column tells you where the Pod is:

  • Init:N/M: N of M init containers have completed. The Pod is waiting on the next one.
  • Init:Error: an init container exited with a non-zero code.
  • Init:CrashLoopBackOff: an init container keeps failing and the kubelet is backing off between retries.
  • Init:ImagePullBackOff (or another Init: reason): an init container is waiting, here for an image it can't pull.
  • Init:ExitCode:N: an init container stopped with exit code N and no reason.
  • PodInitializing: the init containers are done and the application containers are starting.

Start with describe. The Init Containers section shows the state, exit code and restart count for each one, and the events at the bottom show image pull errors, missing volumes or ConfigMaps, and scheduling problems:

Shell
kubectl describe pod web

Then read the logs of the init container that is stuck. Use -c to name it, because kubectl logs without it shows the application container, which has not started yet:

Shell
kubectl logs web -c wait-for-db

If the container has already restarted, the log of the previous attempt is often more useful:

Shell
kubectl logs web -c wait-for-db --previous

The usual causes are a wrong command or script, a dependency that does not exist yet (a Service name, a Secret, a file on a volume), a typo in an image name, or an init container that was meant to be a sidecar but is missing restartPolicy: Always and so never exits. That last one is easy to miss: the Pod sits at Init:0/1 with the container showing as running, and nothing in the events looks wrong.

Once you know what is wrong, fix the manifest and recreate the Pod. Most fields of an existing Pod's containers cannot be changed in place, so delete and re-apply:

Shell
kubectl replace --force -f web.yaml

When not to use an init container

  • The helper must keep running. Use a sidecar (restartPolicy: Always), not an init container.
  • The task should run once per release, not once per Pod. Every replica runs its init containers, so a schema migration in an init container runs as many times as you have Pods. A Job or a Helm hook runs it once: see Helm hooks vs init containers.
  • You want a time limit. activeDeadlineSeconds on the Pod covers init containers, but the docs advise it only for Jobs, because it also kills a Pod that is running correctly.
  • The app already copes. If the app retries its database connection sensibly, a wait loop in front of it adds little.

For the exam

The CKAD (curriculum v1.37) asks you to "Understand multi-container Pod design patterns (e.g. sidecar, init and others)" under Application Design and Build (20%). Expect to add an init container that waits for a Service or prepares a file on a shared volume. On the CKA, a Pod stuck at Init:0/1 or Init:CrashLoopBackOff is a typical Troubleshooting (30%) task: describe, then logs -c.

Common questions

Do init containers run again when a container restarts?

No. If an app container restarts, the init containers do not run again. They do run again if the whole Pod is recreated, for example after a Deployment rollout, or if a container exits with a code that a RestartAllContainers rule matches (Kubernetes 1.36 and later), which restarts the Pod in place and re-runs the init containers in order. Rarer cases exist too, such as the Pod's sandbox being restarted. So make them safe to repeat. Changing an init container's image does not restart the Pod.

Can an init container have probes?

Ordinary init containers cannot have liveness, readiness or startup probes, because they are meant to finish. Native sidecars (restartPolicy: Always) can.

How do resource requests work with init containers?

Because init containers run one at a time, the scheduler uses the larger of (a) the biggest init container request plus the requests of any sidecars listed before it, and (b) the sum of the app and sidecar containers' requests, plus Pod overhead. So order matters: a 100m sidecar listed before a 200m init container counts as 300m during init. The effective Pod requests and limits page on kubernetes.io has the exact rules.

Keep going

The Kubernetes Pod Patterns series:

Init containers make much more sense once you have watched a Pod start step by step. The Pods and multi-container patterns module of the Gazeek CKAD course gives you a real cluster for each lab, including init containers and sidecars, with checks that tell you when you have it right. For exam-style tasks, try the CKAD practice questions with answers, and keep field names fresh with the free Gazeek Memory Gym.

More from the blog