<p>I have a docker image that contains data in directors /opt/myfiles, Lets say the following:
/opt/myfiles/file1.txt
/opt/myfiles/file2.dat</p>
<p>I want to deploy that image to kubernetes and mount an NFS volume to that directory so that the changes to these files are persisted when I delete the pod.</p>
<p>When I do it in docker swarm I simply mount an empty NFS volume to /opt/myfiles/ and then my docker swarm service is started, the volume is populated with the files from the image and I can then work with my service and when I delete the service, I still have the files on my NFS server, so on next start of the service, I have my previous state back.</p>
<p>In kubernetes, when I mount an empty NFS volume to /opt/myfiles/, the pod is started and /opt/myfiles/ is overwritten with an empty directory, so my pod does not see the files from the image anymore.</p>
<p>My volme mount and volume definition:</p>
<pre><code>[...]
volumeMounts:
  - name: myvol
    mountPath: /opt/myfiles
[...]
volumes:
  - name: myvol
    nfs:  
      server: nfs-server.mydomain.org
      path: /srv/shares/myfiles
</code></pre>
<p>I read some threads about similar problems (for example <a href="https://stackoverflow.com/questions/54896056/k8s-doesnt-mount-files-on-persistent-volume">K8s doesn&#39;t mount files on Persistent Volume</a>) and tried some stuff using <code>subPath</code> and <code>subPathExpr</code> as in the documentation (<a href="https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath</a>) but none of my changes does, what docker swarm does by default.</p>
<p>The behaviour of kubernetes seems strange to my as I have worked with docker swarm for quite a while now and I am familiar with the way docker swarm handles that. But I am sure that there is a reason why kubernetes handles that in another way and that there are some possibilities to get, what I need.</p>
<p>So please, can someone have a look at my problem and help me find a way to get the following behaviour?</p>
<ul>
<li>I have files in my image in some directory</li>
<li>I want to mount an NFS volume to that directory, because I need to have the data persisted, when for example my pod crashes and moves to another host or when my pod is temporarily shut down for whatever reason.</li>
<li>When my pod starts, I want the volume to be populated with the files from the image</li>
</ul>
<p>And of course I would be really happy if someone could explain me, why kubernetes and docker swarm behave so different.</p>
<p>Thanks a lot in advance</p>

<p>This can be usually achieved in Kubernetes with init-containers.
Let's have an image with files stored in <code>/mypath</code> folder. If you are able to reconfigure the container to use a different path (like <code>/persistent</code>) you can use init container to copy files from <code>/mypath</code> to <code>/persistent</code> on pod's startup.</p>
<pre><code>containers:
  - name: myapp-container
    image: myimage
    env:
    - name: path
      value: /persistent 
    volumeMounts: 
    - name: myvolume
      path: /persistent
  initContainers:
  - name: copy-files
    image: myimage
    volumeMounts: 
    - name: myvolume
      path: /persistent
    command: ['sh', '-c', 'cp /mypath/*.* /persistent']
</code></pre>
<p>In this case you have a main container <code>myapp-container</code> using files from <code>/persistent</code> folder from the NFS volume and each time when container starts the files from  <code>/mypath</code> will be copied into that folder by init container <code>copy-files</code>.</p>
