2

I want to pass an argument to my dockerfile such that I should be able to use that argument in my run command but it seems I am not able to do so

I am using a simple bash file that will trigger the docker build and docker run

FROM openjdk:8 AS SCRATCH WORKDIR / ADD . . RUN apt install unzip RUN unzip target/universal/rule_engine-1.0.zip -d target/universal/rule_engine-1.0 COPY target/universal/rule_engine-1.0 . ENV MONGO_DB_HOST="host.docker.internal" ENV MONGO_DB_PORT="27017" EXPOSE 9000 ARG path CMD target/universal/rule_engine-1.0/bin/rule_engine -Dconfig.file=$path 

above is my dockerfile and below is my bash file which will access this dockerfile

#!/bin/bash # change the current path to rule engine path cd /Users/zomato/Documents/Intern_Project/rule_engine sbt dist ENVIR=$1 config="" if [ $ENVIR == "local" ] then config=conf/application.conf elif [ $ENVIR == "staging" ] then config=conf/staging.conf else config=conf/production.conf fi echo $config docker build --build-arg path=$config -t rule_engine_zip . docker run -i -t -p 9000:9000 rule_engine_zip 

but when i access the dockerfile through bash script which will set config variable I am not able to set path variable in last line of dockerfile to the value of config.

4
  • Anything you pass in an ARG is compiled into the image – think of docker build as similar to sbt dist in your setup. If you can update your application to look for the file location in an environment variable rather than a Java property, then you can pass the location using the docker run -e option, without rebuilding the image. Commented Mar 10, 2021 at 10:36
  • 1
    Hi @William Martens i don't think this is the problem I am facing which you mentioned in your comment. thanks btw Commented Mar 10, 2021 at 11:53
  • Hi @David Maze thanks for sharing your Knowledge. I will try this Commented Mar 10, 2021 at 11:55
  • @Lazy_Nerd Oh right, thanks for telling! Commented Mar 10, 2021 at 11:57

1 Answer 1

4

ARG values won't be available after the image is built, so a running container won’t have access to those values. To dynamically set an env variable, you can combine both ARG and ENV (since ENV can't be overridden):

ARG PATH ENV P=${PATH} CMD target/universal/rule_engine-1.0/bin/rule_engine -Dconfig.file=$P 

For further explanation, I recommend this article, which explains the difference between ARG and ENV in a clear way:

enter image description here

As you can see from the above image, the ARG values are available only during the image build.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.