0

I have a single file which I want to mount in the container. The file is present in conf folder which also contains other file but the file I only want to mount is helper.conf. Doing this in docker:

docker run -it -v /path/to/dir/:/path/inside/container --name mycontainer dockerimage

Doing this throws below error:

Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type

To resolve this, I created another folder with name config inside conf and used below line:

docker run -it -v /path/to/dir/config:/path/inside/container --name mycontainer dockerimage

This works perfectly fine. Same is happening with kubernetes. Is it not possible to just mount a single file from a directory where other files are also present. Am I using wrong keywords for this.?

How can I resolve in Kubernetes?

Matheus Lacerda
  • 5,983
  • 11
  • 29
  • 45
S Andrew
  • 5,592
  • 27
  • 115
  • 237
  • 1
    I think what you can do is to make a configmap for the file and mount that into the pod - https://stackoverflow.com/a/43404857/9705485 – Ryan Dawson Aug 08 '18 at 08:49
  • @RyanDawson Agree but if we update that file, then we will have to run the command again to make the changes and then redeploy it. Is that true.? – S Andrew Aug 08 '18 at 08:53
  • Yes. Are you running the cluster on the local machine (e.g. minikube)? If so I think you're right that you can only mount directory (e.g. using hostPath https://github.com/kubernetes/minikube/issues/2#issuecomment-406279202). – Ryan Dawson Aug 08 '18 at 08:58

1 Answers1

2

The answer has been provided by @Ryan Dawson.

The best way to mount a single file in container (in Kubernetes) would be to use ConfigMap:

ConfigMaps allow you to decouple configuration artifacts from image content to keep containerized applications portable.

ConfigMap can be used in this case to create a resource which will allow us to keep the configuration separate from the container image. As the configuration is a set of key-value pairs, it will allow to expose it as an environment variable that can be put inside the container or a volume. After creating ConfigMap, you will have to create a pod where you specify a ConfigMap it can consume to get necessary values. In your situation, as Joel B and Tommy Nguyen specified in this Stack Overflow question :

You could use subPath like this to mount single file into existing directory.

aurelius
  • 3,433
  • 1
  • 13
  • 22