gpt4 book ai didi

docker - 根据构建ARG在Dockerfile中选择ENV/ARG

转载 作者:行者123 更新时间:2023-12-02 19:24:17 26 4
gpt4 key购买 nike

我目前有两个相同的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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com