多阶段Dockerfile中的共享变量:FROM前的ARG未被替换

iaqfqrcu  于 2023-03-22  发布在  Docker
关注(0)|答案(2)|浏览(245)

我正在为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变量?

euoag5mw

euoag5mw1#

ARG仅适用于单个映像的构建阶段。对于多阶段,只需声明以下内容即可续订ARG:

ARG DARSHAN_VER

在你的指令之后。
参见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}"
...
zzzyeukh

zzzyeukh2#

以下是文档中的引用:
ARG指令在定义它的构建阶段结束时超出范围。要在多个阶段中使用arg,每个阶段必须包含ARG指令。
https://docs.docker.com/engine/reference/builder/#scope
在FROM之前声明的ARG在构建阶段之外,因此不能在FROM之后的任何指令中使用。要使用在第一个FROM之前声明的ARG的默认值,请使用构建阶段内没有值的ARG指令
https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact

相关问题