Select ENV/ARG in Dockerfile depending on build ARG

xsrf

I currently have two Dockerfiles that are identical except for some ENV vars at the beginning that have different values. I want to combine them into one Dockerfile and select the ENV vars depending on one build-arg / ARG instead.

I tried something like this with $target being either WIN or UNIX:

FROM alpine
ARG target
ARG VAR1_WIN=Value4Win
ARG VAR1_UNIX=Value4Unix
ARG VAR1=VAR1_$target
ARG VAR1=${!VAR1}
RUN echo $VAR1

But it throws an error: failed to process "${!VAR1}": missing ':' in substitution I tried a lot but I'm unable to double expand $VAR1. How do I do this correctly? Thx.

BMitch

For the conditional syntax, there is a pattern you can use with a multi-stage build:

# ARG defined before the first FROM can be used in FROM lines
ARG target

# first base image for WIN target
FROM alpine as base-WIN
# switching to ENV since ARG doesn't pass through to the next stage
ENV VAR1=Value4Win

# second base image for UNIX target
FROM alpine as base-UNIX
ENV VAR1=Value4Unix

# select one of the above images based on the value of target
FROM base-${target} as release
RUN echo $VAR1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related