Recently I was asked “what is the best way to present configuration to a docker container”, my initial answer was to use environment variables and depending on the application one could use a dynamic approach and use ETCD to hold the configuration and have the container software pull it’s configuration from ETCD and have the ability to watch for ETCD configuration changes and preform updates as required. Since then I have being thinking hard to figure if their were other approaches to this problem, one that would work very well is creating a directory on the host, placing the configuration files in the directory and having the directory mounted in the container at run time. My initial reaction to this method was, i did not like it, but the more I though about it I final concluded it is a valid approach and if the correct tooling was put in place, one could have a clean method of presenting any configuration to the container services at run time.
Lets do a quick example, let’s create a config directory in the hosts ‘tmp’ directory, create a config file and then run a docker container interactively and go to the docker/tmp/config directory and see our config file.
Lets create a directory on the host thta we can use as the volume in the container to hold our configuration
Now lets create a quick config file in the /tmp/config directory so we can see it their when we go to the config directory in the container.
Now lets create a Ubuntu container with the the volume mounted
docker run -t -i -v /tmp/config:/tmp/config ubuntu:14.04 /bin/bash
Now we are in the container, you can change directory to the /tmp/config and vitrify that the config directory is present and that you can see the config.conf file. You can monitor this file for changes inside the container and edit/write to this file form the host.