Difficulty: Intermediate
What is an init container in Kubernetes, how does it differ from a regular container, and when would you use one?
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.
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.
| Aspect | Init container | App container |
|---|---|---|
| When it runs | Before app containers, sequentially | After all init containers, in parallel |
| Lifetime | Must exit 0 | Long running |
| Probes | No liveness, readiness or startup probes | Supports all probes |
| Resource accounting | Pod's effective init request is the highest single init request | Requests are summed |
| Image | Can use a different image with tools you do not want in the app image | The 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.
postgres.db resolves or the port opens, so the app does not crash on boot.prisma migrate deploy before the API starts. Make them idempotent, since every replica will run the init container.emptyDir that the app then reads.chown a volume, clone a git repo, or copy static assets.curl or git live in the init image, so the production image stays minimal.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.
Init:0/1 forever, hiding the real problem. Readiness probes and app-level retries are often the better fix.kubectl logs <pod> -c <init-container-name> to see why init failed.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.
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.
Init Containers, Pod Lifecycle, Native Sidecars, restartPolicy, Shared Volumes