K8s doesn't mount files on Persistent Volume

2/26/2019

I have a Docker image and I'd like to share an entire directory on a volume (a Persistent Volume) on Kubernetes.

Dockerfile

FROM node:carbon
WORKDIR /node-test
COPY hello.md /node-test/hello.md
VOLUME /node-test
CMD ["tail", "-f", "/dev/null"]

Basically it copies a file hello.md and makes it part of the image (lets call it my-image).

On the Kubernetes deployment config I create a container from my-image and I share a specific directory to a volume.

Kubernetes deployment

# ...
 spec:
    containers:
    - image: my-user/my-image:v0.0.1
      name: node
      volumeMounts:
      - name: node-volume
        mountPath: /node-test
    volumes:
    - name: node-volume
      persistentVolumeClaim:
        claimName: node-volume-claim

I'd expect to see the hello.md file in the directory of the persistent volume, but nothing shows up.

If I don't bind the container to a volume I can see the hello.md file (with kubectl exec -it my-container bash).

I'm not doing anything different from what this official Kubernetes example does. As matter of fact I can change mountPath and switch to the official Wordpress image and it works as expected.

How can Wordpress image copy all files into the volume directory?

What's in the Wordpress Dockerfile that is missing on mine?

-- a.barbieri
docker
kubernetes
persistent-volumes

1 Answer

2/27/2019

In order not to overwrite the existing files/content, you can use subpath to mount the testdir directory (In the example below) in the existing Container file system.

 volumeMounts:
  - name: node-volume
    mountPath: /node-test/testdir
    subPath: testdir
volumes:
- name: node-volume
  persistentVolumeClaim:
    claimName: node-volume-claim

you can find for more information here using-subpath

-- Suresh Vishnoi
Source: StackOverflow