RikuXan
RikuXan

Reputation: 393

Docker - Share environment variables with referenced volume containers

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.

Upvotes: 1

Views: 2182

Answers (1)

larsks
larsks

Reputation: 312078

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 ...).

Upvotes: 1

Related Questions