300

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

2 Answers 2

431
+300

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 .
4
  • 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
    Commented Dec 14, 2015 at 6:55
  • 3
    what about env variables instead? Commented Dec 1, 2016 at 3:12
  • 33
    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
    Commented Jun 27, 2021 at 20:07
  • 2
    for future readers: make sure to append a --build-arg in front each argument, not just the first, like: docker build --build-arg arg=2.3 --build-arg arg2=3.5 .
    – Adnan
    Commented Jul 2 at 11:11
9

If you have multiple FROM statements in dockerfile, make sure to redeclare the variable/s with no value for each variable/s or they'll be lost when it exits the block.

Here's an article that writes more about this: Why are my Docker ARGs empty?

Here's an example:

ARG VERSION=latest
FROM ubuntu:${VERSION}

ARG VERSION
ARG FILENAME
COPY ${FILENAME} /home/ubuntu/${FILENAME}
1
  • Note that since VERSION isn't reused (in the fragment shown), you don't actually need to repeat "ARG VERSION" after the FROM statement.
    – stevehs17
    Commented Dec 1, 2023 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.