Docker Init Containers

Ravi Mulchandani
3 min readApr 25, 2021

Abstract: In Kubernetes, init containers run before the main app containers are started. This article covers the what, how, and when of the init containers.

1. Introduction

In Object-oriented programming languages, we have a special type of method Constructor. A constructor is used to initialize the fields of the object.
In Kubernetes, we have a special type of containers i.e. init containers.

Init containers are specialized containers that run before the app containers are started in a Pod.

This article describes what are init containers, how to use init containers, and when to use init containers.

2. What are init containers?

In Kubernetes, a pod is a group of one or more containers that share network/storage and specification for how to run containers.

A Pod can have multiple app containers, and one or more, init containers within it.

Init containers execute before other app containers in a Pod. Init containers are used for the initialization of the main app containers hosted on the Pod.

Init containers are specified in the Pod specification which describes the app containers. Init containers can contain utilities that are not included in the actual app containers. Init containers use different images from the containers in a pod.

Once all the init containers are executed, the main app containers in the pod can start. Init containers are executed in the order in which they are declared.

3. How to use init containers?

Init containers are specified in the Pod specification alongside the containers array.

In the following example, wait-for-data is the init container that is responsible for downloading the SQL dump file employee_sample_data.sql from GitHub and place it under location /docker-entrypoint-initdb.d/. We are using the alpine:3.9 image for the init container.

postgres-with-data is the main app container which is using the latest postgres image and initializes the postgres database with the data from employee_sample_data.sql

$ vi initContainers.yamlapiVersion: v1
kind: Pod
metadata:
name: init-container-example
labels:
app: myapp
spec:
initContainers:
- name: wait-for-data
image: alpine:3.9
command:
- "wget"
- "--no-check-certificate"
- "https://raw.githubusercontent.com/rnmulchandani/postgres-sample-data/master/employee_sample_data.sql"
- "-O"
- "/docker-entrypoint-initdb.d/ employee_sample_data.sql"
volumeMounts:
- mountPath: /docker-entrypoint-initdb.d
name: sample-data
containers:
- name: postgres-with-data
image: postgres
env:
- name: POSTGRES_HOST
value: postgres
- name: POSTGRES_PORT
value: "5432"
- name: POSTGRES_PASSWORD
value: "a"
volumeMounts:
- mountPath: /docker-entrypoint-initdb.d
name: sample-data
volumes:
- emptyDir: {}
name: sample-data

To create a pod:

$ kubectl create -f initContainers.yaml

Pod creation process:

$ kubectl get podsNAME                   READY STATUS          RESTARTS AGEinit-container-example 0/1   PodInitializing 0        9s$ kubectl get podsNAME                   READY STATUS  RESTARTS AGEinit-container-example 1/1   Running 0        25s

4. When to use init containers?

Init containers can be used where we want to perform initialization for the main app container.

For example, create the user accounts, create the database schema, modify the configuration, feed the data into the database, download the dependencies, and so on.

5. Conclusion

In the above example, we saw the execution of the main app container was delayed until the SQL dump file is downloaded by the init container.

An init container is a good example of delaying the application execution until one or more dependencies are ready.

6. References

· https://kubernetes.io/docs/concepts/workloads/pods/init-containers/

· https://hub.docker.com/_/postgres

· https://raw.githubusercontent.com/rnmulchandani/postgres-sample-data/master/employee_sample_data.sql

--

--