ARG substitution in RUN command not working for Dockerfile

Another thing to be careful about is that after every FROM statements all the ARGs get collected and are no longer available. Be careful with multi-stage builds.

You can reuse ARG with omitted default value inside FROM to get through this problem:

ARG VERSION=latest
FROM busybox:$VERSION
ARG VERSION
RUN echo $VERSION > image_version

Example taken from docs: https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact


Don't use - in variable names.

Docker build will always show you the line as is written down in the Dockerfile, despite the variable value.

So use this variable name a_version:

ARG a_version

See this example:

Dockerfile:

FROM alpine

ARG a_version
RUN echo $a_version

Build:

$ docker build . --build-arg a_version=1234
Sending build context to Docker daemon 2.048 kB
Step 1/3 : FROM alpine
 ---> a41a7446062d
Step 2/3 : ARG a_version
 ---> Running in c55e98cab494
 ---> 53dedab7de75
Removing intermediate container c55e98cab494
Step 3/3 : RUN echo $a_version                <<< note this <<
 ---> Running in 56b8aeddf77b
1234                                          <<<< and this <<
 ---> 89badadc1ccf
Removing intermediate container 56b8aeddf77b
Successfully built 89badadc1ccf