In a pod can we have a single volume for two different containers.
-
Can you elaborate your question with your purpose? – Shahriar Dec 23 '18 at 09:04
-
I am trying to put nexus and mongo db containers in single pod and with a single volume and up it using minikube. – Kishan Jr Dec 24 '18 at 06:32
-
Can you check my answer? – Shahriar Dec 24 '18 at 06:40
2 Answers
If you have two containers and you want to share data between them, you can do like below:
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: {}
Here,
an emptyDir
is used to share data in between two containers. Both containers has have volume.
So, if you want to share same data, you can mount same volume in two containers.
But, if you want to use single volume, and do not want to share data between two containers, you can use subPath
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: {}
Here,
subPath
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.
In this example, /vol
directory will be mounted in container1
container and /store
from volume will be mounted in container2
Now, you data will not be conflicted and shared

- 13,460
- 8
- 78
- 95
Yes. It's common to share a volume between two containers mainly to communicate. Check out the below diagram.

- 1,073
- 2
- 14
- 27
-
Hi, Sauj if have two images one Nexus and another is MongoDB if I give a single volume won't there we a conflict. Or how does kubernetes figure it out? – Kishan Jr Dec 22 '18 at 09:20