<p>I am migrating my previous deployment made with docker-compose to Kubernetes.
In my previous deployment, some containers do have <strong>some data made at build time</strong> in some paths and these <strong>paths are mounted in persistent volumes</strong>.
Therefore, as the <a href="https://docs.docker.com/storage/volumes/#populate-a-volume-using-a-container" rel="nofollow noreferrer">Docker volume documentation states</a>,<em>the persistent volume (not a bind mount) will be pre-populated with the container directory content</em>.</p>

<p>I'd like to achieve this behavior with Kubernetes and its persistent volumes, How can I do ? Do I need to add some kind of logic using scripts in order to copy my container's files to the mounted path when data is not present the first time the container starts ?</p>

<p>Possibly related question: <a href="https://stackoverflow.com/questions/58128591/kubernetes-mount-volume-on-existing-directory-with-files-inside-the-container">Kubernetes mount volume on existing directory with files inside the container</a></p>

Michel L
<p>I think your options are</p>

<ul>
<li><a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/" rel="noreferrer">ConfigMap</a> (are "some data" configuration files?)</li>
<li><a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/" rel="noreferrer">Init containers</a> (as mentioned)</li>
<li><a href="https://kubernetes.io/docs/concepts/storage/volume-pvc-datasource/#introduction" rel="noreferrer">CSI Volume Cloning</a> (clone combining an init or your first app container)</li>
<li>there used to be a <a href="https://kubernetes.io/docs/concepts/storage/volumes/#gitrepo" rel="noreferrer">gitRepo</a>; deprecated in favour of init containers where you can clone your config and data from</li>
<li><a href="https://kubernetes.io/docs/concepts/storage/volumes/#hostpath" rel="noreferrer">HostPath</a> volume mount is an option too</li>
<li>An <a href="https://kubernetes.io/docs/concepts/storage/volumes/#nfs" rel="noreferrer">NFS volume</a> is probably a very reasonable option and similar from an approach point of view to your Docker Volumes

<ul>
<li>Storage type: NFS, iscsi, awsElasticBlockStore, gcePersistentDisk and others can be pre-populated. There are constraints. NFS probably the most flexible for sharing bits &amp; bytes.</li>
</ul></li>
</ul>

<p>FYI 
The <a href="https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath" rel="noreferrer">subPath</a> might be of interest too depending on your use case and
<a href="https://kubernetes.io/docs/tasks/inject-data-application/podpreset/" rel="noreferrer">PodPreset</a> might help in streamlining the op across the fleet of your pods</p>

<p>HTH</p>
