Reference FROM as variable in RUN

What is the best way to reference a FROM as a variable in a RUN statement? Thank you :).

 FROM alpine:3.12.5
 RUN ...... \
 ...... \
 echo on `date` $FROM was used > log

contents of log

 on 2/2/2022 alpine:3.12.5 was used

I am afraid it’s not going to work like that, but you could declare an ARG with for the base image:

ARG BASE_IMAGE=alpine:3.12.5
FROM ${BASE_IMAGE}
RUN echo "on $(date) ${BASE_IMAGE} was used"

That should do the trick

1 Like

Thank you very much :).

That would not work this way, but it is almost right. You can’t use a build argument in a stage which was only defined outside of any stage. So you have to define it this way:

ARG BASE_IMAGE=alpine:3.12.5
FROM ${BASE_IMAGE}
ARG BASE_IMAGE
RUN echo "on $(date) ${BASE_IMAGE} was used"

The second declaration does not need a value

1 Like

Good catch :slight_smile: Indeed I forget that it behaves like that :blush: