// Module included in the following assemblies:
//
// * nodes/nodes-containers-init.adoc

:_mod-docs-content-type: PROCEDURE
[id="nodes-containers-init-creating_{context}"]
= Creating Init Containers

[role="_abstract"]
You create an init container by creating a pod spec that contains an `initContainers` configuration. 

The following example outlines a simple pod which has two init containers. The first init continer waits for the `myservice` service to complete. After that, the second waits for `mydb` service to complete. After both init containers complete, the pod begins.

.Procedure

. Create the pod for the Init Container:

.. Create a YAML file similar to the following:
+
[source,yaml]
----
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  securityContext:
    runAsNonRoot: true
    seccompProfile:
      type: RuntimeDefault
  containers:
  - name: myapp-container
    image: registry.access.redhat.com/ubi9/ubi:latest
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
    securityContext:
      allowPrivilegeEscalation: false
      capabilities:
        drop: [ALL]
  initContainers:
  - name: init-myservice
    image: registry.access.redhat.com/ubi9/ubi:latest
    command: ['sh', '-c', 'until getent hosts myservice; do echo waiting for myservice; sleep 2; done;']
    securityContext:
      allowPrivilegeEscalation: false
      capabilities:
        drop: [ALL]
  - name: init-mydb
    image: registry.access.redhat.com/ubi9/ubi:latest
    command: ['sh', '-c', 'until getent hosts mydb; do echo waiting for mydb; sleep 2; done;']
    securityContext:
      allowPrivilegeEscalation: false
      capabilities:
        drop: [ALL]
----

.. Create the pod by using the following command:
+
[source,terminal]
----
$ oc create -f myapp.yaml
----

.. View the status of the pod by using the following command:
+
[source,terminal]
----
$ oc get pods
----
+
.Example output
[source,terminal]
----
NAME                          READY     STATUS              RESTARTS   AGE
myapp-pod                     0/1       Init:0/2            0          5s
----
+
The pod status, `Init:0/2`, indicates it is waiting for the two services.

. Create the `myservice` service.

.. Create a YAML file similar to the following:
+
[source,yaml]
----
kind: Service
apiVersion: v1
metadata:
  name: myservice
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9376
----

.. Create the pod by using the following command:
+
[source,terminal]
----
$ oc create -f myservice.yaml
----

.. View the status of the pod by using the following command:
+
[source,terminal]
----
$ oc get pods
----
+
.Example output
[source,terminal]
----
NAME                          READY     STATUS              RESTARTS   AGE
myapp-pod                     0/1       Init:1/2            0          5s
----
+
The pod status, `Init:1/2`, indicates it is waiting for one service, in this case the `mydb` service.

. Create the `mydb` service:

.. Create a YAML file similar to the following:
+
[source,yaml]
----
kind: Service
apiVersion: v1
metadata:
  name: mydb
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9377
----

.. Create the pod by using the following command:
+
[source,terminal]
----
$ oc create -f mydb.yaml
----

.. View the status of the pod by using the following command:
+
[source,terminal]
----
$ oc get pods
----
+
.Example output
[source,terminal]
----
NAME                          READY     STATUS              RESTARTS   AGE
myapp-pod                     1/1       Running             0          2m
----
+
The pod status, `Running`, indicates that it is no longer waiting for the services and is running.
