Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share variable in multi-stage Dockerfile: ARG before FROM not substituted

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

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 

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.

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

like image 289
Alberto Chiusole Avatar asked Dec 08 '18 10:12

Alberto Chiusole


People also ask

How do I pass ARG in Dockerfile?

ARG instruction defines a variable that can be passed at build time. Once it is defined in the Dockerfile you can pass with this flag --build-arg while building the image. We can have multiple ARG instruction in the Dockerfile. ARG is the only instruction that can precede the FROM instruction in the Dockerfile.

What is the difference between ARG and ENV in Dockerfile?

ENV is for future running containers. ARG for building your Docker image. ¶ ENV is mainly meant to provide default values for your future environment variables.

What is ARG command in Dockerfile?

You can use the ARG command inside a Dockerfile to define the name of a parameter and its default value. This default value can also be overridden using a simple option with the Docker build command.

Can you have two from statements in Dockerfile?

With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don't want in the final image.


1 Answers

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

ARG DARSHAN_VER 

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}" ... 
like image 50
Richard Barber Avatar answered Sep 19 '22 04:09

Richard Barber