<p>The work directory of my docker image is set to &quot;/opt&quot;,and it will contain the jar file and config files,etc. I am trying to mount it pvc in k8s, but find the pvc will overwrite the /opt directory.Is there any way to mount the directory in docker image which is not empty , Thanks.</p>

James Pei
<p>When a PVC is mounted to a directory, instead of adding a new file it will overwrite the directory you specify. For your case when the PVC is mounted the directory “<code>/opt</code>” will be replaced. To prevent this you need to create a new directory within the <code>/opt</code> directory or you can mount PVC to a different directory other than the current directory.</p>
<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  storageClassName: standard
  volumeMode: Filesystem
  volumeName: my-pvc
  mountPath: &quot;/opt/my-pvc&quot;
</code></pre>
<p>This YAML will create a new directory named '<code>my-pvc</code>' within the '<code>/opt</code>' directory and mount the PVC to it. Any files that are already in the '<code>/opt</code>' directory will not be overwritten.</p>
<p>Or else:</p>
<pre><code>mountOptions:
    - path: /mnt
</code></pre>
<p>This will mount the PVC to the <code>/mnt</code> directory instead of <code>/opt</code>, so the data in <code>/opt</code> will not be overwritten.</p>
<p>This <a href="https://medium.com/hackernoon/mount-file-to-kubernetes-pod-without-deleting-the-existing-file-in-the-docker-container-in-the-88b5d11661a6" rel="nofollow noreferrer">blog by Iman Tumorang</a> will help you for reference.</p>
