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

查看:30
本文介绍了多阶段 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,这样我就可以只定义一次默认构建变量.如果我在两个阶段复制 ARG,就在两个 FROM 的下方,它会正确构建.

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.

参见.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天全站免登陆