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

Pre-requisite

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@192.168.0.23 ~
$ docker build -t ajeetraina:v1 --build-arg version=`uname -r` .

Verifying the environmental variable

[node1] (local) root@192.168.0.23 ~
$ docker run ajeetraina:v1 env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=b7de0a2c76f5
version=4.4.0-139-generic
HOME=/root