Get image tag inside Dockerfile

Viewed 959

I have a Dockerfile that needs to download a specific version of a software from an URL with the ENV command. Such as:

In Dockerfile:

FROM ubuntu

ENV sw_ver=1.2.3

ADD https://some_address/_name_$sw_ver /some_dir

The build command is analogous to this:

docker build -t my_image:1.2.3 .

Now, it would be great if instead of setting the ENV variable, I could build different images for different versions of the software by simply varying the tag in my docker build command. So is it possible to read the tag number inside the Dockerfile and use it as a variable?

2 Answers

Use a bash script with the tag wanted in the first positional argument of the script:

#!/bin/bash
docker build --build-arg sw-ver=$1 -t my_image:$1 .

Then tun the script:

script.sh 1.2.3
Related