我目前有两个相同的Dockerfile,除了开头的一些ENV变量具有不同的值。我想将它们组合到一个Dockerfile中,然后根据一个build-arg / ARG选择ENV变量。

我用$ target或WIN或UNIX尝试过这样的事情:

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

但这会引发错误:failed to process "${!VAR1}": missing ':' in substitution我做了很多尝试,但无法将$VAR1扩展一倍。
如何正确执行此操作?谢谢。

最佳答案

对于条件语法,可以在多阶段构建中使用一种模式:

# 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

关于docker - 根据构建ARG在Dockerfile中选择ENV/ARG,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57516575/

10-16 18:22