<p>I am running a docker image that has certain configuration files within it. I need to persist/mount the same folder to the disk as new files will get added later on. When I use standard volume mount in kubernetes, it mounts an empty directory without the intial configuration files. How do I make sure my initial files are copied to the volume while mounting?</p>

<pre><code>        - mountPath: /tmp
          name: my-vol
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
      - name: my-vol
        persistentVolumeClaim:
          claimName: wso2-disk2```
</code></pre>

<p>A possible solution could be the use the node storage mounted on containers (easiest way) or using a DFS solution like NFS, GlusterFS, and so on. </p>

<p>Another and recommended way to achieve what you need is to use a persistent volumes to share the same files between your containers. </p>

<p>Assuming you have a kubernetes cluster that has only one Node, and you want to share the path <code>/mtn/data</code> of your node with your pods (<a href="https://stackoverflow.com/a/60092545/12153576">Source</a>):</p>

<p><strong>Create a PersistentVolume:</strong></p>

<blockquote>
  <p>A hostPath PersistentVolume uses a file or directory on the Node to emulate network-attached storage.</p>
</blockquote>

<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
</code></pre>

<p><strong>Create a PersistentVolumeClaim:</strong></p>

<blockquote>
  <p>Pods use PersistentVolumeClaims to request physical storage</p>
</blockquote>

<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi
</code></pre>

<p>Look at the PersistentVolumeClaim:</p>

<p><code>kubectl get pvc task-pv-claim</code></p>

<p>The output shows that the PersistentVolumeClaim is bound to your PersistentVolume,  <code>task-pv-volume</code>.</p>

<pre><code>NAME            STATUS    VOLUME           CAPACITY   ACCESSMODES   STORAGECLASS   AGE
task-pv-claim   Bound     task-pv-volume   10Gi       RWO           manual         30s
</code></pre>

<p><strong>Create a deployment with 2 replicas for example:</strong></p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      volumes:
        - name: task-pv-storage
          persistentVolumeClaim:
            claimName: task-pv-claim
      containers:
        - name: task-pv-container
          image: nginx
          ports:
            - containerPort: 80
              name: "http-server"
          volumeMounts:
            - mountPath: "/mnt/data"
              name: task-pv-storage
</code></pre>

<p>Now you can check inside both container the path <code>/mnt/data</code> has the same files.</p>

<p>If you have cluster with more than 1 node I recommend you to think about the other types of <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/" rel="nofollow noreferrer">persistent volumes</a> or using <a href="https://kubernetes.io/docs/concepts/storage/" rel="nofollow noreferrer">DFS</a>. </p>

<p><strong>References:</strong>
<a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/" rel="nofollow noreferrer">Configure persistent volumes</a>
<a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/" rel="nofollow noreferrer">Persistent volumes</a>
<a href="https://kubernetes.io/docs/concepts/storage/volumes/#types-of-volumes" rel="nofollow noreferrer">Volume Types</a></p>
