Demonstrating Build Time Variable in Dockerfile using ARG

Docker allows you to set both build time and run time ENV variables and even lets you overwrite build time ENV vars at run time. There may come a time where you’ll want to add an ENV variable to your Docker image, but you’ll want this value to be different depending on where you build your image, but you don’t want to edit your Dockerfile to change it.You may also want to sometimes overwrite that value at run time (not build time).

The ARG instruction defines a variable that users can pass at build-time to the builder with the docker build command using the –build-arg = flag.

Tested Infrastructure

Platform Number of Instance Reading Time
Play with Docker 1 5 min


Create a Dockerfile

Setting up a Docker image with a build argument and ENV variable:

$ cat Dockerfile
FROM ubuntu:latest
ARG version
ENV version $version

Building Docker Image

$[node1] (local) root@ ~
$ docker build -t ajeetraina:v1 --build-arg version=`uname -r` .

Verifying the environmental variable

[node1] (local) root@ ~
$ docker run ajeetraina:v1 env