<p>In a pod can we have a single volume for two different containers.</p>

Kishan Jr
<p>If you have two containers and you want to share data between them, you can do like below:</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: production
spec:
  containers:
  - name: container1
    image: image1
    volumeMounts:
    - name: storage
      mountPath: /vol/data
  - name: container2
    image: image2
    volumeMounts:
    - name: storage
      mountPath: /store/data
  volumes:
  - name: storage
    emptyDir: {}
</code></pre>

<p>Here,</p>

<p>an <code>emptyDir</code> is used to share data in between two containers. Both containers has have volume.</p>

<p>So, if you want to share same data, you can mount same volume in two containers.</p>

<p>But, if you want to use single volume, and do not want to share data between two containers, you can use <code>subPath</code></p>

<pre><code>spec:
  containers:
  - name: container1
    image: image1
    volumeMounts:
    - name: storage
      mountPath: /vol/data
      subPath: vol
  - name: container2
    image: image2
    volumeMounts:
    - name: storage
      mountPath: /store/data
      subPath: store
  volumes:
  - name: storage
    emptyDir: {}
</code></pre>

<p>Here,</p>

<p><code>subPath</code> specify a sub-path inside the referenced volume instead of its root. That means, two separate directory from your volume will be mounted in two containers. </p>

<p>In this example, <code>/vol</code> directory will be mounted in <code>container1</code> container and <code>/store</code> from volume will be mounted in <code>container2</code></p>

<p>Now, you data will not be conflicted and shared</p>
