<p>I'm trying to make use of init container to prepare some files before the main container starts up. In the init container I'd like to mount a <code>hostPath</code> volume so that I can share prepare some file for the main container.</p>

<p>My cluster is using pre 1.6 version of kubernetes so I'm using the <code>meta.annotation</code> syntax:</p>

<pre><code>pod.beta.kubernetes.io/init-containers: '[
    {
        "name": "init-myservice",
        "image": "busybox",
        "command": ["sh", "-c", "mkdir /tmp/jack/ &amp;&amp; touch cd /tmp/jack &amp;&amp; touch a b c"],
        "volumeMounts": [{
          "mountPath": "/tmp/jack",
          "name": "confdir"
        }]
    }
]'
</code></pre>

<p>But it doesn't seem to work. The addition of <code>volumeMounts</code> cause the container <code>init-myserver</code> go into CrashLoop. Without it the pod gets created successfully but it doesn't achieve what I want.</p>

<p>Is it not possible in &lt;1.5 to mount volume in init container?
What about 1.6+?</p>

<p>You don't need to do <code>hostPath</code> volume to share data generated by init-container with the containers of Pod. You can use <code>emptyDir</code> to achieve same result. The benefit of using <code>emptyDir</code> is that you don't need to do anything on host and this will work on any kind of cluster even if you don't have access to nodes on that cluster.</p>

<p>Another set of problems with using <code>hostPath</code> is setting proper permissions on that folder on host also if you are using any SELinux enabled distro you have to setup the right context on that directory.</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: init
  labels:
    app: init
  annotations:
    pod.beta.kubernetes.io/init-containers: '[
        {
            "name": "download",
            "image": "axeclbr/git",
            "command": [
                "git",
                "clone",
                "https://github.com/mdn/beginner-html-site-scripted",
                "/var/lib/data"
            ],
            "volumeMounts": [
                {
                    "mountPath": "/var/lib/data",
                    "name": "git"
                }
            ]
        }
    ]'
spec:
  containers:
  - name: run
    image: docker.io/centos/httpd
    ports:
      - containerPort: 80
    volumeMounts:
    - mountPath: /var/www/html
      name: git
  volumes:
  - emptyDir: {}
    name: git
</code></pre>

<p>Checkout the above example where the init-container and the container in pod are sharing same volume named <code>git</code>. And the type of volume is <code>emptyDir</code>. I just want the init-container to pull the data everytime this pod comes up and then it is served from the <code>httpd</code> container of pod.</p>

<p>HTH.</p>
