πŸ“šBook Signing at KubeCon EU 2026Meet us at Booking.com HQ (Mon 18:30-21:00) & vCluster booth #521 (Tue 24 Mar, 12:30-1:30pm) β€” free book giveaway!RSVP Booking.com Event
Configuration intermediate ⏱ 15 minutes K8s 1.28+

Kubernetes Init Containers Guide

Use Kubernetes init containers to run setup tasks before your main application starts. Covers database migrations, config generation, dependency

By Luca Berton β€’ β€’ πŸ“– 5 min read

πŸ’‘ Quick Answer: configuration

The Problem

This is a fundamental Kubernetes topic that engineers search for frequently. A comprehensive reference with production-ready examples saves hours of trial and error.

The Solution

Init Container Example

apiVersion: v1
kind: Pod
metadata:
  name: web-app
spec:
  initContainers:
    # 1. Wait for database to be ready
    - name: wait-for-db
      image: busybox:1.36
      command: ['sh', '-c', 'until nc -z postgres.default 5432; do echo waiting for db; sleep 2; done']

    # 2. Run database migrations
    - name: run-migrations
      image: my-app:v1
      command: ['python', 'manage.py', 'migrate']
      env:
        - name: DATABASE_URL
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: url

    # 3. Download config from external source
    - name: fetch-config
      image: curlimages/curl:8.5.0
      command: ['sh', '-c', 'curl -o /config/app.json https://config.example.com/app.json']
      volumeMounts:
        - name: config
          mountPath: /config

  containers:
    - name: app
      image: my-app:v1
      volumeMounts:
        - name: config
          mountPath: /config
          readOnly: true

  volumes:
    - name: config
      emptyDir: {}

Init Container Rules

RuleDescription
Run sequentiallyInit container 1 must complete before 2 starts
Must succeedIf any init container fails, pod restarts
Run to completionEach init container must exit 0
Share volumesInit and main containers can share emptyDir volumes
Different imageCan use specialized tools not in main image

Common Use Cases

Use CaseInit Container
Wait for dependencync -z service port loop
Database migrationRun migration script
Download config/certscurl or wget
Set file permissionschmod/chown on volumes
Clone git repogit clone into shared volume
Register with serviceAPI call to register
graph LR
    A[Init 1: wait-for-db] -->|Success| B[Init 2: migrate]
    B -->|Success| C[Init 3: fetch-config]
    C -->|Success| D[Main container starts]
    A -->|Failure| E[Pod restarts]

Frequently Asked Questions

Init containers vs startup probes?

Init containers run separate containers for setup tasks. Startup probes check if the main container’s app is ready. Use init containers for external dependencies, startup probes for slow-starting apps.

Can init containers access secrets and configmaps?

Yes β€” init containers have the same access to volumes, secrets, configmaps, and service accounts as main containers.

Best Practices

  • Start with the simplest configuration that meets your needs
  • Test changes in staging before production
  • Use kubectl describe and events for troubleshooting
  • Document your decisions for the team

Key Takeaways

  • This is essential Kubernetes knowledge for production operations
  • Follow the principle of least privilege and minimal configuration
  • Monitor and iterate based on real-world behavior
  • Automation reduces human error and improves consistency
#init-containers #startup #migrations #dependencies #kubernetes
Luca Berton
Written by Luca Berton

Principal Solutions Architect specializing in Kubernetes, AI/GPU infrastructure, and cloud-native platforms. Author of Kubernetes Recipes and creator of CopyPasteLearn courses.

Kubernetes Recipes book cover

Want More Kubernetes Recipes?

This recipe is from Kubernetes Recipes, our 750-page practical guide with hundreds of production-ready patterns.

Luca Berton Ansible Pilot Ansible by Example Open Empower K8s Recipes Terraform Pilot CopyPasteLearn ProteinLens