4

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.

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)

Mick
  • 413
  • 4
  • 14
  • Is the volume only for the frontend static files? Is there any other data being updated on the volume? – Matt Jan 30 '20 at 05:39
  • Where is your yaml files? – BMW Jan 30 '20 at 05:53
  • Does this answer your question? [Kubernetes PVC deleting the contents of the POD](https://stackoverflow.com/questions/55613111/kubernetes-pvc-deleting-the-contents-of-the-pod) – adrihanu Aug 26 '20 at 10:08

1 Answers1

2

Kubernetes (really the container runtime underneath) bind mounts 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.

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

If you do need the shared data, you could add an initContainer to your deployment and it can manage the copy of data to the volume. For complex scenarios, rsync will usually have a flag for what you need to do.

  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

It sounds like you are hosting an SPA. 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.

FROM docker.io/node:12 AS build
WORKDIR /app
COPY . /app/
RUN yarn install && yarn build

FROM docker.io/nginx:1.17
COPY --from=build /app/dist/. /usr/share/nginx/html/
CMD [ "nginx", "-g", "daemon off;" ]
Matt
  • 68,711
  • 7
  • 155
  • 158
  • Hi Matt, thanks for the response. I am running a 'python manage.py collectstatic' command in my Dockerfile that builds the app. Worst case I could solve this by running the command only on the first startup of the container, but would rather have all the files generated beforehand in the image. – Mick Jan 30 '20 at 08:02
  • in this case, how should the volume be mounted for the actual container? at `/apt/dist`? – bluesmonk Jun 21 '22 at 13:27