0
0
Dockerdevops~10 mins

Init container pattern in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Init container pattern
Pod starts
Init Container runs
Init Container completes successfully?
NoPod waits
Yes
Main Container starts
Pod runs normally
The pod starts by running the init container first. Only after it finishes successfully does the main container start.
Execution Sample
Docker
apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  initContainers:
  - name: init-myservice
    image: busybox
    command: ['sh', '-c', 'echo Init task; sleep 3']
  containers:
  - name: main-app
    image: nginx
This pod runs an init container that prints a message and waits 3 seconds before starting the main nginx container.
Process Table
StepActionInit Container StateMain Container StatePod State
1Pod startsNot startedNot startedStarting
2Init container starts runningRunningNot startedWaiting for init container
3Init container prints message and sleepsRunningNot startedWaiting for init container
4Init container completes successfullyCompletedNot startedInit container done
5Main container startsCompletedRunningRunning
6Main container runs nginx serverCompletedRunningRunning
7Pod runs normallyCompletedRunningRunning
💡 Init container completes successfully, so main container starts and pod runs normally.
Status Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
Init Container StateNot startedRunningCompletedCompletedCompleted
Main Container StateNot startedNot startedNot startedRunningRunning
Pod StateStartingWaiting for init containerInit container doneRunningRunning
Key Moments - 3 Insights
Why does the main container not start immediately when the pod starts?
Because the pod waits for the init container to finish first, as shown in execution_table step 2 and 4 where the main container remains 'Not started' until the init container completes.
What happens if the init container fails?
The pod will keep waiting and not start the main container. This is implied in the flow where if the init container does not complete successfully, the pod waits.
Can the init container run multiple times?
No, the init container runs once before the main container starts, as shown in the execution_table where it runs and completes before the main container starts.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of the main container at Step 3?
ANot started
BRunning
CCompleted
DFailed
💡 Hint
Check the 'Main Container State' column at Step 3 in the execution_table.
At which step does the pod start running the main container?
AStep 2
BStep 4
CStep 5
DStep 7
💡 Hint
Look at the 'Main Container State' column to see when it changes to 'Running'.
If the init container never completes, what will happen to the pod state?
APod runs normally
BPod waits indefinitely
CPod starts main container anyway
DPod crashes immediately
💡 Hint
Refer to the concept_flow where the pod waits if the init container does not complete.
Concept Snapshot
Init container pattern in Kubernetes pods:
- Pod starts with init container first
- Init container runs setup tasks
- Main container waits until init container finishes
- If init container fails, pod waits
- Ensures main container starts only after setup is done
Full Transcript
The init container pattern means a pod runs a special container first to prepare things. The pod starts, then the init container runs. The main container waits until the init container finishes successfully. If the init container fails, the pod keeps waiting and does not start the main container. This ensures the main container only runs after the setup is complete. In the example, the init container prints a message and sleeps for 3 seconds before finishing. Then the main nginx container starts and runs normally.