gpt4 book ai didi

docker - 在多阶段 Dockerfile : ARG before FROM not substituted 中共享变量

转载 作者:行者123 更新时间:2023-12-04 03:06:20 25 4
gpt4 key购买 nike

我正在为 darshan utils 编写一个多阶段 Dockerfile :

ARG DARSHAN_VER=3.1.6

FROM fedora:29 as build
RUN dnf install -y \
gcc \
make \
bzip2 bzip2-devel zlib zlib-devel
RUN curl -O "ftp://ftp.mcs.anl.gov/pub/darshan/releases/darshan-${DARSHAN_VER}.tar.gz" \
&& tar ...


FROM fedora:29
COPY --from=build "/usr/local/darshan-${DARSHAN_VER}" "/usr/local/darshan-${DARSHAN_VER}"
...

我用 docker build -t darshan-util:3.6.1 . 构建它,我得到的错误是:
Step 5/10 : RUN curl -O "ftp://ftp.mcs.anl.gov/pub/darshan/releases/darshan-${DARSHAN_VER}.tar.gz"     && tar ...

---> Running in 9943cce1669c
% Total % Received % Xferd Average Speed Time Time Time Current
...
curl: (78) RETR response: 550
The command '/bin/sh -c curl -O "ftp://ftp.mcs.anl.gov/pub/darshan/releases/darshan-${DARSHAN_VER}.tar.gz" && tar ...' returned a non-zero code: 78

我想在两个阶段重用相同的 ARG,以便我可以只定义一次默认构建变量。
如果我在两个阶段都复制 ARG,就在两个 FROM 的下方,它会正确构建。

使用默认值定义“全局”多级 ARG 变量的正确方法是什么?

最佳答案

ARG 仅在单个镜像的构建阶段持续。
对于多级,通过简单地说明更新 ARG:

ARG DARSHAN_VER

在您的 FROM 指令之后。

参见 https://docs.docker.com/engine/reference/builder/#arg
ARG DARSHAN_VER=3.1.6

FROM fedora:29 as build
ARG DARSHAN_VER
RUN dnf install -y \
gcc \
make \
bzip2 bzip2-devel zlib zlib-devel
RUN curl -O "ftp://ftp.mcs.anl.gov/pub/darshan/releases/darshan-${DARSHAN_VER}.tar.gz" \
&& tar ...


FROM fedora:29
ARG DARSHAN_VER
COPY --from=build "/usr/local/darshan-${DARSHAN_VER}" "/usr/local/darshan-${DARSHAN_VER}"
...

关于docker - 在多阶段 Dockerfile : ARG before FROM not substituted 中共享变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53681522/

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