<p>I have a question about Kubernetes containers and persistent volumes.</p>
<p><strong>How can I make some of the preexisting folders of a Kubernetes container persistent?</strong></p>
<p>I know the usage of PVCs in Kubernetes but the problem about mounting a PVC to a container is that this operation -naturally- deletes everything in the mount path. eg. Say that we have an image which has a non-empty directory <code>/xyz</code> and we want to make this directory persistent. If we create a PVC and mount it to <code>/xyz</code>, we would lose everything inside <code>/xyz</code> as well (we don't want this to happen). So we want that directory to be persistent from the start with the files inside of it.</p>
<p>I'm not so sure if Docker or any other container technology responds such a feature, so it may not be suitable for Kubernetes too. Would be glad if anyone can enlighten me about this. Thanks!</p>
<p>My approaches so far:</p>
<ul>
<li><em>Copying</em>: Creating a PVC for the directory contents and mounting it to an init container or job that copies <code>/xyz</code> to the <code>/mounted/xyz</code>, then mounting PVC to the main container's <code>/xyz</code>. This approach has some drawbacks if the directory is too fat or has some OS/runtime-specific configurations.</li>
<li><em>Hostpath</em>: Populating a directory with the contents of <code>/xyz</code> (eg. <code>/in/host/xyz</code>) before starting the container. Then mounting this path from host to the container. Not a good approach since it's hard to automate.</li>
</ul>

tuna
<p>there is no way to mount a Volume in a certain folder without overwriting its contents.</p>
<p>In my opinion the best approaches could be:</p>
<ol>
<li><p>The first one reported by you (for large content):</p>
<p>a. Create PVC</p>
<p>b. Add an initContainer to your Deployment that mount the Volume in a DIFFERENT path from the directory containing the data to move/copy</p>
<p>c. Add to the initContainer a &quot;command&quot; field with the commands to move/copy the content from the &quot;source&quot; directory to the mounted volume (target)</p>
<p>d. Mount to the &quot;main&quot; container the PVC used in the initContainer at the &quot;source&quot; directory path</p>
</li>
<li><p>Create a K8s cronjob (or job that works once if the files are never modified) that syncs from one folder to another (similar to point 1, but avoid waiting a long time before the application Pod starts, since the initContainer is no longer needed).
<a href="https://i.stack.imgur.com/EoWMU.png" rel="nofollow noreferrer">Cronjob example</a>
(Pay attention to file owners; you may need to run the job under the same serviceAccount that produced those files)</p>
</li>
<li><p>If they are static files, build the Docker image with all the contents of the folder already inside (Dockerfile —&gt; copy). <a href="https://docs.docker.com/engine/reference/builder/" rel="nofollow noreferrer">https://docs.docker.com/engine/reference/builder/</a></p>
</li>
</ol>
<p>I strongly recommend not using hostPath in PRODUCTION environments.
<a href="https://kubernetes.io/docs/concepts/storage/volumes/#hostpath" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/storage/volumes/#hostpath</a></p>
