I've been playing with a little toy that uses docker-compose to bring together a web app, couchdb, and redis container into an easy-ier-ish cohesive unit.
While working on it (and to make it a bit more generic), my next step was to find a way to pass the database admin user/pass (and other configuraiton options) into the containers as environment variables which took me way longer to figure out than it should have...
Hopefully this posts helps it click for you a little faster than it (didn't) for me :)
If you land here, you've likely already poured over the different parts of documentation for docker, docker-compose and environment variables.
In case things drift in the product or docs, this post was written using
docker-compose version 1.7.1, build 0a9ab35 so keep that in mind...
I think the difficult thing for me was piecing the various ways you can get environment variables defined and the necessary mapping required within the
Environment Variable Setup Stages.
For me it didn't click until I was able to think about the stages that needed to exist for an environment variable to go from the development computer -> to the -> docker container.
For now I'm thinking of using the following model...
------------------------ -------------------- ------------------
| Env Source | | docker-compose.yml | | Docker Container |
| | | | | |
| A) .env file | --> | map env vars using | --> | echo $DEMO_VAR |
| B) run-time terminal | | interpolation | | |
| env var | | in this file. | | |
------------------------ --------------------- ------------------
A working example.
If you want to see all of this in one place check out this github example which is outline below.
The example above is layed out like so...
This is where you can place each of the environment variables you need in here.
DEMO_VAR=Test value from .env file!
As the docs say you can use
# as comments and blank lines in the file - all other lines must be in the format of
environment variables in your terminal's context will take president over the values in the
The above file is the part where I got tripped up, and once I added the
environment: section it all clicked.
You likely don't want every one of your development or production server's environment variables to show up inside your container. This file acts a bit like the
docker run -e ENV_VAR=FOO option and allows you to select specific environment variables that are to be passed into the container.
I like the declarative approach of this file as it makes environment variable dependencies explicit.
ENV DEMO_VAR WAT
COPY docker-entrypoint.sh /
Dockerfile, but one thing I learned is you can setup default environment variables using the docker
ENV directive. But these will be overriden by the
.env file or variables in your terminal's environment.
echo "ENV Var Passed in: $DEMO_VAR"
This was just a sample script to print out the environment variable.
Some other things I learned
The docs say you can specify your own
env-file or even multiple files, however I could not get that working. It always wanted to choose the
Also note: that if you have an environment variable specified in your terminal that also exists in your
.env file the terminal's environment takes precedence over the
Happy Environment Setup!