<p>I just started exploring kubernetes concepts. I am using helm chart to do deployment of pod. Got stucked to solve the below problem. Anyone kindly help me to unblock this issue.</p>
<p>I have three containers, lets say A, B and C. container A is having a file =&gt; &quot;/root/dir1/sample.txt&quot;. Otherwise I can prepare this file offline but needs to be mounted in all three containers. Container A and container C has a service running which will update that file. Container B has a service running which will read that file. so, I need this file to be shared across all the three containers.</p>
<p>Approach 1:
Using emptyDir volume, I tried mounting this file but it doesn't helped my case. Because when I use emptyDir I am losing all the files under dir1 which comes via container images. Also I don't want other files under dir1 to be shared across containers. In addition to that I need to mount the file as same directory structure &quot;/root/dir1/sample.txt&quot; instead of creating empty dir say &quot;/root/dir2&quot; as shared-data mount and copying sample.txt to dir2 as &quot;/root/dir2/sample.txt&quot;. Reason is now mounting /root/dir2 in all three containers (file write happening in any container reflected in other containers). But this does not helped my case as my need is to mount the file as same directory structure &quot;/root/dir1/sample.txt&quot;.</p>
<p>Approach 2:
Using configmap volume, I can mount the file &quot;sample.txt&quot; under dir1 as expected. But it is mounted as read-only filesystem and containers A and C are unable to write the file.</p>
<p>So above two approaches does not helped my case. It would be great if anyone help me on how to mount a file directly into containers with write access under same directory structure and shared across containers. Or any volume type available in kubernetes will help my case (<a href="https://kubernetes.io/docs/concepts/storage/volumes/#cinder" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/storage/volumes/#cinder</a>).</p>
<p>Thanks in advance!</p>

<p>I think your best option is to use an emptyDir and an initContainer that mounts it to another path and copies the original files into it. Its not an uncommon pattern. See it in action in <a href="https://github.com/rabbitmq/cluster-operator/commit/60b048fde7d7314801d34d051f88a7577640efdb" rel="noreferrer">rabbitmq cluster operator</a>.</p>
<p>That would look something like this in the pod:</p>
<pre><code>(...)
spec:
  volumes:
    - name: shared-dir
      emptyDir: {}
  initContainers:
    - name: prepare-dir
      image: YOUR_IMAGE
      command:
        - sh
        - '-c'
        - 'cp /root/dir1 /tmp/dir1'
      volumeMounts:
        - name: shared-dir
          mountPath: /tmp/dir1/
  containers:
    - name: container-a
      image: YOUR_IMAGE
      volumeMounts:
        - name: shared-dir
          mountPath: /root/dir1/
    - name: container-b
      image: YOUR_IMAGE
      volumeMounts:
        - name: shared-dir
          mountPath: /root/dir1/
(...)
</code></pre>
