It is an unfortunate constraint that you only have this "dev/qa/prod" environment variable. However, it is possible to achieve what you want.
First, you might consider baking your environment specific configuration into the image for all environments. (Normally I would discourage to do this!)
For example you can COPY
three files into your image:
dev-env.sh
: contains your dev config in the form:
ELASTICSEARCH_URL=http://elastic-dev:123
qa-env.sh
(similar)
prod-env.sh
(similar)
Then you evaluate at run-time (not at build-time) in which environment you are: You add an ENTRYPOINT
script to your image which will source the correct file, depending on the ENVIRONMENT_NAME
variable.
Dockerfile (part):
ENTRYPOINT ["docker-entrypoint.sh"]
docker-entrypoint.sh (copied into WORKDIR
of the image):
#!/bin/bash
set -e
if [ "$ENVIRONMENT_NAME" = "prod" ]; then
source prod-env.sh
fi
# else if qa ... , else if dev ..., else fail
exec "$@"
This script will run when you launch the docker container, so this approach is no option for you if you need the variables to be available in Dockerfile-instructions (at build-time).
Another (build-time) workaround is described here and consists of using temporary files to store environment variables across multiple image layers.