Kubernetes Init Container is a container that runs before the main application containers in a pod. It performs setup work that must finish successfully before the application starts, such as preparing configuration, waiting for a dependency, running a database migration, or copying files into a shared volume.
Init containers are part of the pod spec. Kubernetes runs them in order, one at a time. When every init container exits successfully, Kubernetes starts the app containers.
An init container handles startup tasks that do not belong inside the long-running application process. Common tasks include:
emptyDir volume shared with the main container.Init containers are defined under spec.initContainers in a Pod manifest. Kubernetes starts the first init container and waits for it to complete with exit code 0. Then it starts the next one. If any init container fails, Kubernetes retries it based on the pod restart policy.
The app containers under spec.containers do not start until all init containers complete successfully.
A simplified pod flow looks like this:
Suppose your app needs a database to be reachable before it starts. You can use an init container to wait for the database service:
apiVersion: v1
kind: Pod
metadata:
name: app-with-init
spec:
initContainers:
- name: wait-for-db
image: busybox:1.36
command:
- sh
- -c
- until nc -z postgres 5432; do echo waiting for db; sleep 2; done
containers:
- name: app
image: my-app:1.0.0
ports:
- containerPort: 8080
In this example, the app container starts only after the wait-for-db init container can connect to postgres:5432.
An init container is meant to run and exit. An app container is meant to keep running.
Because init containers must complete, they do not use readiness, liveness, or startup probes in the same way long-running app containers do.
A sidecar container runs alongside the main application container. It usually supports the app while it runs, such as a log shipper, proxy, or local agent.
An init container runs before the app and then stops. Use an init container for setup work. Use a sidecar for ongoing work.
curl, nc, or migration CLIs if only the init container uses them.Init:CrashLoopBackOff.Init containers affect deployment behavior. If you deploy Kubernetes resources with tools such as Terraform, make sure your manifests define init containers, volumes, Secrets, and ConfigMaps in the right order. This is especially relevant when you deploy Kubernetes resources using Terraform.
For workloads on managed Kubernetes services like Amazon EKS, init containers work the same way as in standard Kubernetes. They are useful for platform workloads such as Airflow, where setup tasks may need to prepare connections, metadata, or config before schedulers and workers start. A similar pattern appears when teams deploy Apache Airflow on AWS EKS.
During cluster upgrades, check init container images and commands as carefully as app containers. Old images, deprecated APIs, or missing permissions can break pod startup after an upgrade. This should be part of your Kubernetes upgrade checklist.
A startup runs a Node.js API on Kubernetes. The app needs a generated config file that includes a cluster-specific internal URL and a mounted certificate. Instead of adding shell tools to the Node.js image, the team adds an init container based on a small Linux image. The init container reads the mounted Secret, writes /config/app.yaml into an emptyDir volume, then exits. The Node.js container mounts the same volume and starts with the generated config.
This keeps the runtime image smaller and makes the setup step visible in pod status. If the Secret is missing, the pod fails during initialization instead of starting the API with invalid config.
A Kubernetes init container is the right tool for ordered, one-time setup inside a pod. Use it when the main application should not start until a dependency, file, permission, or configuration step is complete.