在多阶段Dockerfile中共享变量:未替换FROM之前的ARG [英] Share variable in multi-stage Dockerfile: ARG before FROM not substituted

查看:81
本文介绍了在多阶段Dockerfile中共享变量:未替换FROM之前的ARG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing a multi-stage Dockerfile for the darshan utils:

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.构建它,得到的错误是:

I build it with docker build -t darshan-util:3.6.1 . and the error I get is:

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,这样我就可以一次定义一个默认的构建变量.如果我在两个阶段中都在两个FROM的正下方复制ARG,则会正确构建.

I'd like to reuse the same ARG in both stages, so that I can define a default build variable just once. If I duplicate ARG in both stages, just below the two FROMs, it builds correctly.

使用默认值定义全局"多阶段ARG变量的正确方法是什么?

What is the correct way to define a "global" multi-stage ARG variable with a default?

推荐答案

ARG仅在单个图像的构建阶段持续.对于多阶段,只需说明以下内容即可更新ARG:

ARGs only last for the build phase of a single image. For the multistage, renew the ARG by simply stating:

ARG DARSHAN_VER

在您的FROM指示之后.

after your FROM instructions.

cf. 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}"
...

这篇关于在多阶段Dockerfile中共享变量:未替换FROM之前的ARG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆