In today’s article we will be looking at how you can clone your application code into the Container running in the Kubernetes Container platform. This serves as an ideal solution if you store application code in Git version control and would like to pull the latest code during deployment without rebuilding container image. A kubernetes feature which allows us to perform this operation is Init Containers.

Init Containers are specialized type of containers that run before application containers in a Pod. These containers can contain utilities or setup scripts not present in an application image. There is nothing so unique about Init containers as they can be specified in the Pod specification alongside the containers array.

In our example we will deploy an example nginx container whose web application data is pulled from a Git repository using Init container. Note that a Pod can have multiple containers running applications within it, but it can also have one or more init containers.

My Setup requirements

These are the container images which will be used in this example:

  • alpine/git : Run as Init container for git pull operation
  • nginx: Runs Nginx web server

I’ll create a demo namespace for this test:

$ kubectl create ns helloworld
namespace/helloworld created

I have a Git repository with Hello World HTML file:

Create Kubernetes Pod deployment manifest

We will generate a template and modify it to add Init container.

kubectl run nginx-helloworld  --image nginx --restart=Never --dry-run -o yaml >nginx-helloworld-pod.yml

If you want to get Kubernetes Deployment YAML file run:

kubectl run  nginx-helloworld  --image nginx  --dry-run -o yaml >nginx-helloworld-deploy.yml

Here is the Pod deployment file I got.

$ cat nginx-helloworld-pod.yml 
apiVersion: v1
kind: Pod
  creationTimestamp: null
    run: nginx-helloworld
  name: nginx-helloworld
  - image: nginx
    name: nginx-helloworld
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

I’ll update the manifest file contents to look like below.

apiVersion: v1
kind: Pod
    run: nginx-helloworld
  name: nginx-helloworld
  - image: nginx
    name: nginx-helloworld
    - containerPort: 80
    - mountPath: "/usr/share/nginx/html"
      name: www-data
  - name: git-cloner
    image: alpine/git
        - clone
        - --single-branch
        - --
        - /data
    - mountPath: /data
      name: www-data
  - name: www-data
    emptyDir: {}

Notice we’re performing the following:

  • Using init container called git-cloner to clone git repository to /data
  • /data is a mount of the volume named www-data. This enables sharing between containers
  • The www-data volume is mounted to /usr/share/nginx/html in the nginx container. For the web data we cloned to reside in default root directory.

Let’s apply the file to create Kubernetes resources.

$ kubectl apply -f nginx-helloworld-pod.yml -n helloworld
pod/nginx-helloworld created

Confirm the pods are created.

$ kubectl get pods -n helloworld

Here is a complete screenshot with confirmation that clone happened and data was placed in the path as mounted.

nginx kubernetes git clone

When using persistent volume claim, you’ll update the volumes section to something like below.

    - name: my-pv-storage
        claimName: mypv-claim

Refer to the Persistent Volumes task page on Kubernetes Documentation to learn more about persistent storage.

Clean up:

kubectl delete all --all -n helloworld
kubectl delete ns helloworld

Kubernetes learning videos:

Your support is our everlasting motivation,
that cup of coffee is what keeps us going!

As we continue to grow, we would wish to reach and impact more people who visit and take advantage of the guides we have on our blog. This is a big task for us and we are so far extremely grateful for the kind people who have shown amazing support for our work over the time we have been online.

Thank You for your support as we work to give you the best of guides and articles. Click below to buy us a coffee.


Please enter your comment!
Please enter your name here