QA
Intermediate LevelDocker & Deployment

What are Init Containers and when are they used in Kubernetes pods?

❓ Interview Prompt: Explain Kubernetes init containers — sequential pre-startup execution, database migration patterns, and dependency readiness waiting.

Init Containers run to completion before any regular (app) containers in the pod start. They are perfect for pre-startup tasks that must succeed before the application begins.

Key Properties:

•
Run sequentially in order (init-1 completes → init-2 starts → ... → app starts).
•
If an init container fails, the pod restarts (respecting restart policy) until it succeeds.
•
Init containers can use different images from app containers (e.g., using migrate-tool image for DB migrations without including it in the app image).
•
Init containers share volumes with app containers — useful for seeding files.

Common Use Cases:

1
Wait for dependencies: Spin-wait until a database/service is ready before the app starts.
2
Database migrations: Run npm run migrate using the same code but ensure only one pod runs migrations.
3
File seeding: Download config files or certificates from external sources into a shared volume.
4
Security setup: Fetch Vault secrets and write them to shared tmpfs volume.

vs. Startup Probes:

Startup probes delay liveness/readiness evaluation within the same container. Init containers run tasks in completely separate containers before the app container starts.

Code Implementation Example

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api
spec:
  template:
    spec:
      initContainers:
        # Init 1: Wait for PostgreSQL to accept connections
        - name: wait-for-postgres
          image: postgres:16-alpine
          command:
            - sh
            - -c
            - |
              until pg_isready -h postgres-svc -p 5432 -U appuser; do
                echo "Waiting for postgres..."
                sleep 2
              done
              echo "PostgreSQL is ready!"
        
        # Init 2: Run database migrations
        - name: db-migrate
          image: myapp:v1.2.3     # same image as app
          command: ["npm", "run", "migrate"]
          env:
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: url
      
      # Main app starts ONLY after both init containers succeed
      containers:
        - name: api
          image: myapp:v1.2.3
          ports:
            - containerPort: 3000
💡Two init containers: wait for postgres readiness, then run DB migrations — app starts only after both complete.
🎯 Interview Key Takeaway

Init containers run sequentially to completion before app containers. Use them for dependency waiting, DB migrations, and setup tasks — keeping these concerns out of the application image.

#Kubernetes#Init Containers#Database Migrations#Dependencies#Intermediate
Back to all Docker & Deployment Questions→

More Docker & Deployment Interview Questions

Beginner LevelDocker & Deployment

How do Docker Multi-Stage Builds minimize image size and improve security?

Beginner LevelDocker & Deployment

What is the difference between a Docker Container and a Virtual Machine (VM)?

Beginner LevelDocker & Deployment

What is the difference between a Docker Image and a Docker Container?