<p>Try to share a file between two containers within a pod, I must use volume and create file in mouth path for this volume.</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: pod-5
spec:
  containers:
  - name: busybox
    image: busybox
    command: ["sh", "-c", "if [ -f /workdir/foo ]; then sleep 3600; else exit; fi"]
    volumeMounts:
    - name: workdir
      mountPath: "/workdir"
  initContainers:
  - name: install
    image: busybox
    command: ["sh", "-c", "touch /workdir/foo; hostname &gt; /workdir/foo"]
    volumeMounts:
    - name: workdir
      mountPath: "/workdir"
  volumes:
  - name: workdir
    emptyDir: {}
</code></pre>

<p>If I don't use volume and create a file in init container and try to read it from the other container, it will not work.</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: pod-5
spec:
  containers:
  - name: busybox
    image: busybox
    command: ["sh", "-c", "if [ -f /workdir/foo ]; then sleep 3600; else exit; fi"]
  initContainers:
  - name: install
    image: busybox
    command: ["sh", "-c", "touch /workdir/foo; hostname &gt; /workdir/foo"]
</code></pre>

<p>Why? I thought all containers within a pod should share both network and file system. </p>

<p>Containers within same pod share network namespace and IPC namespace but they have separate mount namespace and filesystem.Hence we use volumes  for sharing mounts. To know more about namespaces check the linux namespace <a href="https://man7.org/linux/man-pages/man7/namespaces.7.html" rel="noreferrer">doc</a>.</p>
