Kubernetes mount volume on existing directory with files inside the container Kubernetes mount volume on existing directory with files inside the container kubernetes kubernetes

Kubernetes mount volume on existing directory with files inside the container


Unfortunately Kubernetes' volume system is very different from Docker's so this is not possible directly. If there is a single file (or a small number) you can use subPath projection like this:

volumeMounts:- name: cephfs-0  mountPath: /opt/myapplication/conf/foo.conf  subPath: foo.conf

Repeat that for each file. But if you have a lot of files, or if they can vary, then you have to handle this at runtime or use templating tools. Usually that means mounting it somewhere else and setting up symlinks before your main process starts.


I also encountered this very niche issue not being able to mount a folder to a specific path with content from my built image. This ends up empty.

However my workaround is to use ENTRYPOINT in de dockerfile refering to a shellscript that runs the commands to initialize DB or do something with files that affects the mounted target folder.

So it seems that entrypoint runs after the volume is being mounted by kubernetes.

I did tried to symlink the path in the entrypoint script, but that didn't work out.