3

I have a situation where I need to set an ENV based on runtime condition like thus:

RUN if [ "$RUNTIME" = "prod" ] then VARIABLE="Some Production URL"; else VARIABLE="Some QA URL"; fi;
ENV={VARIABLE}

Been looking at different solutions but none of them seem to be panning out (for example the basic one where VARIABLE is lost when RUN exits). What would be an elegant way to achieve this?

4
  • Possible duplicate of Conditional Block in Docker
    – eLRuLL
    Dec 13, 2018 at 23:04
  • 1
    Not the same. I Need a way to set ENV dynamically! the use case is kibana app talking to an elasticsearch backend. During different Runtime stages of our CICD process, i need to configure differernt ElasticSearch urls using `ENV ELASTICSEARH_URL=Dev/QA/Prod Elastic URLS' Dec 14, 2018 at 1:35
  • It seems that an script can help you with this. Depending of the value of the RUNTIME Dec 14, 2018 at 4:46
  • that is what i was thinking - but how would i set an ENV inside a sh script? the problem didn't seem to be getting solved Dec 14, 2018 at 11:49

3 Answers 3

3

The literal conditional execution can be achieved with multistage build and ONBUILD.

ARG mode=prod

FROM alpine as img_prod
ONBUILD ENV ELASTICSEARH_URL=whatever/for/prod

FROM alpine as img_qa
ONBUILD ENV ELASTICSEARH_URL=whatever/for/qa

FROM img_${mode}
...

Then you build with docker build --build-arg mode=qa .

5
  • Interesting - I have not come across any suggestion thus. At the end there's just one dockerfile? Dec 14, 2018 at 15:43
  • @DorianMcAllister yes. this method is quite unorthodox.
    – Siyu
    Dec 14, 2018 at 15:49
  • I see..also - is bash scripting something that can be done for ENV similar to RUN? Was thinking around something like this ENV ELASTICSEARCH_URL=if [ "$env = "prod" ] echo "PRODURL"; else echo "QAURL"; fi; Dec 14, 2018 at 16:00
  • 1
    just add ONBUILD before run.
    – Siyu
    Dec 14, 2018 at 16:01
  • I didn't know about ONBUILD, this is really useful, thanks @Siyu
    – kenske
    May 12, 2022 at 16:09
1

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.

0

Wouldn't passing env var with docker run be the solution you need? Something like this:

docker run -e YOUR_VARIABLE="Some Production URL" ...

2
  • we dont pass runtime. its passed by our CICD pipelines that we dont own. the only runtime envs we have are the deploy environment dev/qa/prod Dec 14, 2018 at 11:48
  • Why don't you determine the Elasticsearch URL just before starting Kibana? Either in entrypoint or in any other scipt you use. Check there your ENV and configure Kibana properly.
    – Izydorr
    Dec 15, 2018 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.