Expose folder from a container as a volume in a Kubernetes Pod

10/2/2019

I have a pod which contains two containers. One container is a web application and another store some static data for this web application.

The data here is a set of files which are stored in the folder of this container with name /data and that's only function of the container to store this data and expose them to the web application.

I'm looking for the way to share the content of this folder with web application container in this pod.

If I'm using the YAML spec below the folder in both containers is empty. Is there a way to share the data from container folder without cleaning it up?

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  labels:
    app: my-app
    version: 1.2.3
spec:
  volumes:
    - name: my-app-data-volume

  containers:
    - name: my-app-server
      image: my-app-server-container-name
      volumeMounts:
        - name: my-app-data-volume
          mountPath: /data
      ports:
        - containerPort: 8080

    - name: my-app-data
      image: my-app-data-container-name
      volumeMounts:
        - name: my-app-data-volume
          mountPath: /data
-- Alexey Usharovski
docker
docker-volume
kubernetes

1 Answer

10/2/2019

You can use an EmptyDir volume for this. Specify the container that contains the files as an initContainer, then copy the files into the EmptyDir volume. Finally, mount that volume in the web app container.

-- Jamie
Source: StackOverflow