I am trying to use a multistage build and getting an invalid reference format on the second FROM, which if I hardcode the FROM it works as expected… my question is what is the proper way to use ARG B to atore the version so can be used by the second FROM? Thank you :).
ARG A=1
FROM alpine:${A} as build
WORKDIR /path/to
RUN .....
ARG B=2
FROM alpine:${B}
WORKDIR /path/to
COPY from=build / /
Also tried to renew ARG B in the build.
ARG A=1
ARG B=2
FROM alpine:${A} as build
ARG B=$B
WORKDIR /path/to
RUN .....
ARG B=${B}
FROM alpine:${B}
WORKDIR /path/to
COPY from=build / /
You should be able to use the arguments in the FROM instructions even if you defined them at the beginning of the file. This is actually the only way. Everything that you define after the FROM instruction will be available only in that stage and not in the following stages.
If you want to use the variable not in the FROM instruction but for example in a RUN instruction, you can set the values before any FROM and just refer the variable again wihout setting it:
ARG A=1
ARG B=2
ARG C=3
FROM alpine:${A} as build
FROM alpine:${B}
ARG C
RUN echo "${C}"