kubernetes initcontainers prepopulate file

---
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  # step 1: define directory on a host node as volume, which will be used by init container to populate file and then by app to read prepopulated file
  volumes:
    - name: html
      emptyDir: {}

  # step 2: before starting app, prepare file
  initContainers:
    - name: init
      image: alpine

      # mount given volume to `/data`
      volumeMounts:
        - name: html
          mountPath: /data

      # write to mounted volume
      command:
        - sh
        - -c
        - |
          echo '<h1>HELLO WORLD</h1>' > /data/index.html

  # step 3: app
  containers:
    - name: nginx
      image: nginx:alpine

      # mount prepopulated volume
      volumeMounts:
        - name: html
          mountPath: /usr/share/nginx/html

To run this example

kubectl apply -f nginx.yml
kubectl wait --for=condition=ready pod nginx
kubectl exec nginx cat /usr/share/nginx/html/index.html
kubectl delete -f nginx.yml