How to mount a single file in container in kubernetes/docker

8/8/2018

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?

-- S Andrew
containers
docker
host
kubernetes
mount

1 Answer

8/9/2018

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
Source: StackOverflow