1

Is there a possibility to let Docker share the environment variables of containers that are linked with --volumes-from at runtime, the same way it does with containers linked with --link?

I'm using data-only containers for persistent data in my applications and I would like to check in the entrypoint script of my application container whether or not a data container was referenced at runtime and cancel if there is none.
My idea was checking whether or not the environment variable of the data container (containing the path of the exposed volume) was set, I was also planning on using its value at certain points in the script (e.g. setting the data directory of my application in its configuration files).

Is there any way to achieve this and if not what would be the next best way to do the things I described?


I found that docker inspect returns all volumes-from containers under HostConfig.VolumesFrom. This does not completely solve the sharing problem since I would like to check for a fixed value (the environment variable's name) and not a container name and need the variable's value as well, but it's a start.

RikuXan
  • 393
  • 5
  • 15
  • You could save your dot files in the images. – Shawn K Sep 09 '15 at 01:54
  • @Kosch What dot-files do you mean exactly? – RikuXan Sep 09 '15 at 02:01
  • Like .bashrc, .bash_profile, etc. places to set variables and other fun stuff. the answers below are really good and speak to separation of concerns related to organizing your configurations. Basically you need a way to get you config on the machine. So it's just a matter of getting the right file to the right place at the right time. – Shawn K Sep 09 '15 at 02:09
  • I'm not sure I can follow you, I want to share the environment variables between two containers that are connected by the means of `--volumes-from`. The configuration files of my main machine are not the problem. – RikuXan Sep 09 '15 at 02:11

1 Answers1

1

Is there any way to achieve this and if not what would be the next best way to do the things I described?

You cannot expose environment variables from one container to another using --volumes-from.

Your application shouldn't necessarily care whether or not a data container was referenced at runtime. You should declare a VOLUME in your Dockerfile so that either (a) the application will initialize a new data store using the anonymous volume that results from the VOLUME directive, or will (b) make use of a volume presented using wither --volumes-from or using -v on the docker run command line.

You could simply provide the location of the data volume as an environment variable when you start the application container (-e DATA_PATH=/path/to/my/data).

If you care whether the data volume has been "initialized" in some fashion, you can check the DATA_PATH location for a specific flag file of some sort (if ! [ -f "$DATA_PATH/flagfile" ]; then ...).

larsks
  • 277,717
  • 41
  • 399
  • 399
  • I actually want the data container to be mandatory to encourage separation of application and data for similar reasons as described in http://stackoverflow.com/questions/18496940/how-to-deal-with-persistent-storage-e-g-databases-in-docker and the referenced articles. Using runtime environment variables is a good idea, however I feel like they don't really enforce the data containers as they can be used with runtime volumes as well. – RikuXan Sep 09 '15 at 02:00