<p>I have a simple StatefulSet with two containers. I just want to share a path by an emptyDir volume:</p>
<pre><code>volumes:
 - name: shared-folder
 emptyDir: {}
</code></pre>
<p>The first container is a busybox:</p>
<pre><code>  - image: busybox
    name: test
    command:
      - sleep
      - &quot;3600&quot;
    volumeMounts:
    - mountPath: /cache
      name: shared-folder
</code></pre>
<p>The second container creates a file on /cache/&lt;POD_NAME&gt;. I want to mount both paths within the emptyDir volume to be able to share files between containers.</p>
<pre><code>  volumeMounts:
    - name: shared-folder
      mountPath: /cache/$(HOSTNAME)
</code></pre>
<p><strong>Problem.</strong> The second container doesn't resolve /cache/$(HOSTNAME) so instead of mounting /cache/pod-0 it mounts /cache/$(HOSTNAME). I have also tried getting the POD_NAME and setting as env variable but it doesn't resolve it neither.</p>
<p>Dows anybody knows if it is possible to use a path like this (with env variables) in the mountPath attribute?</p>

<p>To use mountpath with env variable you can use <a href="https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath-expanded-environment" rel="nofollow noreferrer">subPath with expanded environment variables</a> (k8s v1.17+).</p>
<p>In your case it would look like following:</p>
<pre><code>containers:
- env:
  - name: MY_POD_NAME
    valueFrom:
      fieldRef:
        fieldPath: metadata.name
  volumeMounts:
  - mountPath: /cache
    name: shared-folder
    subPathExpr: $(MY_POD_NAME)
</code></pre>
