Dockerfile: ARG

FROM instructions support variables that are declared by any ARG instructions that occur before the first FROM.

An ARG declared before a FROM is outside of a build stage, so it can’t be used in any instruction after a FROM.

FROM instructions is the only instructions that support variable declared by ARG before the first FROM.

ARG my-ver=1.0.0
FROM image:$my-ver	# This will interpret to FROM image:1.0.0
RUN echo "version: $my-ver"	# It will NOT work on this line. It will print.. `version: `

To use the default value of an ARG declared before the first FROM use an ARG instruction without a value inside of a build stage.

To use ARG declared before the first FROM in other instructions, redeclare the variable without any value.

ARG my-ver=1.0.0
FROM image:$my-ver	# This will interpret to FROM image:1.0.0
ARG my-ver
RUN echo "version: $my-ver"	# It will print.. `version: 1.0.0`

Once you have Dockerfile setup, you can pass the arguments with:

docker build --build-arg my-ver=2.0.0 .

References:


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *