FoxuTech

How to Clone a Git Repository using init-container

How to Clone a Git Repository using init-container

In this article, will see how to clone a git repository using init-container. For that, let’s use an init container to clone the git repository before running any other application container. The github repository contains application data, which will be used by application. After successfully cloning application data from the github repository, the init-container will be terminated. And finally, application will start to execute.

What is Init Container?

A pod can have Init Containers in addition to application containers. Init containers allow you to reorganize setup scripts and binding code.

An init container is the one that starts and executes before other containers in the same Pod. It’s meant to perform initialization logic for the main application hosted on the Pod.

Properties of Init container

Init-container in use

Using emptyDir volume

#Pod with Init-container using emptyDir volume
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: foxapp
  name: foxapp
spec:
  containers:
  - image: foximage
    name: fox-app
    volumeMounts:
    - name: app-data
      mountPath: /usr/share/fox/html
  initContainers:
  - image: alpine/git
    name: git
    command:
    - git
    - clone
    - https://github.com/xxxxxxxxx/yyyyyyyyyyyyy.git
    - /temp-repo
    volumeMounts:
    - name: app-data
      mountPath: /temp-repo
  volumes:
  - name: app-data
    emptyDir: {}

Using hostPath volume

#Pod with Init-container using hostPath volume
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: foxapp
  name: foxapp
spec:
  containers:
  - image: foximage
    name: fox-app
    volumeMounts:
    - name: app-data
      mountPath: /usr/share/fox/html
  initContainers:
  - image: alpine/git
    name: git
    command:
    - git
    - clone
    - https://github.com/xxxxxxxxx/yyyyyyyyyyyyy.git
    - /temp-repo
    volumeMounts:
    - name: app-data
      mountPath: /temp-repo
  volumes:
  - name: app-data
    hostPath:
      path: /root/app
      type: DirectoryOrCreate

Use cases of Init container

Exit mobile version