<p>My app pod and nginx pod share a persistent volume for static files. When the app image is built with docker, the static files are generated in the location that the static persistent volume will later bind to when the containers are deployed. </p>

<p>However it seems that during the deployment all the files are wiped off the static directory. Does this have to do with the order of the deployment? Ex: the persistent volume has to be applied first, then the nginx deployment (because the static mounted directory is blank at first), then the app (which already has files in the mounted directory)</p>

<p>Kubernetes (really the container runtime underneath) <a href="https://unix.stackexchange.com/a/198591/22470">bind mounts</a> a volume/directory from the host into the container. All files and sub directories in the containers target directory are hidden under the contents of the directory that is now mounted on top.</p>

<p>Do you really need a shared volume for this data? Is any of it being updated at runtime? </p>

<p>If you do need the shared data, you could add an <code>initContainer</code> to your deployment and it can manage the copy of data to the volume. For complex scenarios, <a href="https://linux.die.net/man/1/rsync" rel="nofollow noreferrer"><code>rsync</code></a> will usually have a flag for what you need to do.</p>

<pre><code>  initContainers:
  - name: app
    image: my/nginx:1.17
    volumeMounts:
    - name: app-data
      mountPath: /app/data
  - name: app-data
    image: my/app:3.1.4
    command: ["sh", "-c", "cp -r /app/dist/. /app/data"]
    volumeMounts:
    - name: app-data
      mountPath: /app/data
</code></pre>

<p>It sounds like you are hosting an <a href="https://en.wikipedia.org/wiki/Single-page_application" rel="nofollow noreferrer">SPA</a>. If the shared volume is simply to get the static built files into the nginx container, maybe inject the files into web server container at build rather than dealing with volumes.</p>

<pre><code>FROM docker.io/node:12 AS build
WORKDIR /app
COPY . /app/
RUN yarn install &amp;&amp; yarn build

FROM docker.io/nginx:1.17
COPY --from=build /app/dist/. /usr/share/nginx/html/
CMD [ "nginx", "-g", "daemon off;" ]
</code></pre>
