271

I am using RUN instruction within a Dockerfile to install a rpm

RUN yum -y install samplerpm-2.3

However, I want to pass the value "2.3" as an argument. My RUN instruction should look something like:

RUN yum -y install samplerpm-$arg

where $arg=2.3

techraf
  • 64,883
  • 27
  • 193
  • 198
meallhour
  • 13,921
  • 21
  • 60
  • 117

1 Answers1

401

As of Docker 1.9, You are looking for --build-arg and the ARG instruction.

Check out this document for reference. This will allow you to add ARG arg to the Dockerfile and then build with

docker build --build-arg arg=2.3 .
Gulzar
  • 23,452
  • 27
  • 113
  • 201
Andy Shinn
  • 26,561
  • 8
  • 75
  • 93
  • 2
    After doing all that, I am getting an error **No package samplerpm-$arg available. ** It seems the argument value of 2.3 is not getting substituted. – meallhour Dec 14 '15 at 06:55
  • 3
    what about env variables instead? – Alexander Mills Dec 01 '16 at 03:12
  • 27
    Environment variables are a _runtime_ variable. The build arguments are a _build_ time variable. The distinction here is declaring variables that you only want to use for building the image and do not want (or should not) store for runtime of the container. – Andy Shinn Jun 27 '21 at 20:07