AKS 4 – What is an Init Container in Kubernetes?

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 This:

You’re opening a shop every morning. Before customers arrive, someone needs to:

  • Unlock the doors
  • Clean the floor
  • Set up the cash register

Only after all this is done, the shop is ready for business.

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:

  1. Download dependencies before app starts (e.g., config files, certs)
  2. Wait for a service to be ready (e.g., database)
  3. Set permissions or mount volumes
  4. 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
    If any init container fails, the pod won’t start — just like a shop not opening if the keys are missing.

Example:

apiVersion: v1

kind: Pod

metadata:

  name: init-demo

spec:

  containers:

  – name: main-app

    image: busybox

    command: [‘sh’, ‘-c’, ‘echo Main app is running && sleep 3600’]

  initContainers:

  – name: init-setup

    image: busybox

    command: [‘sh’, ‘-c’, ‘echo Doing setup… && sleep 5’]

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.

Leave a Reply

Your email address will not be published. Required fields are marked *