Mefitico
Mefitico

Reputation: 1116

Get image tag inside Dockerfile

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?

Upvotes: 6

Views: 4150

Answers (2)

paltaa
paltaa

Reputation: 3254

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

Upvotes: 3

Martijn Gastkemper
Martijn Gastkemper

Reputation: 667

You can use build-args to change env vars during build time.

docker build --build-arg sw_ver=4.5.6 -t my_image:1.2.3 .

Docker documentation about build-args

Upvotes: 0

Related Questions