<p>I have an NFS physical volume that my pods can all access via a PVC, files are kept after pods are destroyed.</p>
<p>I want each pod to be able to put its files under a unique subdirectory.</p>
<p>Is there anyway that I can dynamically utilize say <code>metadata.uid</code> or <code>metadata.name</code> in the <code>mountPath</code> for the container? i.e. conceptually this:</p>
<pre class="lang-yaml prettyprint-override"><code>volumeMounts:

  - name: persistent-nfs-storage
    mountPath: /metadata.name/files
</code></pre>
<p>I think I can see how to handle first making the directory, by using an init container and putting the value into the environment using the downward API.  But I don't see any way to utilize it in a PVC mountPath.</p>
<p>Thanks for any help.</p>

thisisnotatest
<p>I don't know if it is possible to use Pod Name in Volume mountPath. But, if the intention is writing files in a separate folder(using pod name) of the same PVC, there are workarounds.</p>
<p>One way to achieve it is by getting the file path and pod name from env and then append them. After that write the log on that directory.</p>
<p>In details,</p>
<pre><code>volumeMounts:
- name: persistent-nfs-storage
  mountPath: /nfs/directory
</code></pre>
<p>ENVs:</p>
<pre><code>env:
- name: WRITE_PATH
  value: &quot;$(NFS_DIR)/$(POD_NAME)&quot;
- name: NFS_DIR
  value: /nfs/directory
- name: POD_NAME
  valueFrom:
    fieldRef:
      fieldPath: metadata.name
</code></pre>
<p>In Application, use <code>$WRITE_PATH</code> directory to write your necessary files. Also, if necessary create this directory from init container.</p>
