Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker set ENV based on if-else

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?

like image 609
Dorian McAllister Avatar asked Oct 26 '25 07:10

Dorian McAllister


1 Answers

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 .

like image 171
Siyu Avatar answered Oct 28 '25 22:10

Siyu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!