An Init Container is a special type of container that runs before the main application container starts in a Pod.
Think of it like a “setup step” before your app runs.
Use Case (in simple terms):
Imagine you’re baking a cake (your main app) — but before you start, you need to:
- Preheat the oven
- Clean the bowl
- Bring ingredients
These setup tasks = Init Containers
Baking the cake = Main Application Container
✅ Why and when to use Init Containers:
- Download dependencies before app starts (e.g., config files, certs)
- Wait for a service to be ready (e.g., database)
- Set permissions or mount volumes
- Run DB migration scripts safely before app starts
Key points:
- Runs in order (can have multiple Init Containers)
- Each must succeed before the next or main container runs
- Runs only once per pod start
Example:
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
initContainers:
– name: init-myservice
image: busybox
command: [‘sh’, ‘-c’, ‘echo waiting for DB; sleep 10’] containers:
– name: main-app
image: myapp:latest
ports:
- containerPort: 80
This pod first runs the init container that waits 10 seconds, then starts your main app.
Init container when success then only main container run….it return zero code which indicate its success.