Neymar
Neymar

Reputation: 41

How to verify build args is set properly in Dockerfile

I've got the following line in Dockerfile:

ARG COOL_ID
...
ENTRYPOINT ["java", "-jar", "/usr/share/java/${COOL_ID}/app.jar"]

but when I run it there's an error:

Error: Unable to access jarfile /usr/share/java//app.jar

and I can see that my ${COOL_ID} argument was not formatted correctly.

How can I fix it?

Upvotes: 2

Views: 3927

Answers (1)

Adiii
Adiii

Reputation: 59976

It will not substitute the variable, as Docker treat ENTRYPOINT and CMD as a command so processing variables like shell will not work. Try to change the CMD to run it as a shell and then you will able to process variable the same as the shell.

Also, you can not use ARG in CMD to treat them as an environment variable, you just use them inside Dockerfile, to use them as an environment variable you must assign them to some ENV.

ARG COOL_ID
ENV COOL_ID=$COOL_ID

I will also suggest to verify and check COOL_ID in Docker build time, if not set then it should print the warning or error to the user, see below example if ARG not passed to builds params then it will print an error message to the user.

ARG COOL_ID
#see ARG is for build time 
RUN if [  -z $COOL_ID ];then \
  >&2 echo  "\n****************Warning!!!!*************\n"; \
  >&2 echo "COOL_ID seems empty!" ;\
  fi

ENV COOL_ID=$COOL_ID
# ENV is for run time
CMD ["sh", "-c", "java -jar /usr/share/java/${COOL_ID}/app.jar"]

Now build the docker with --build-arg

docker build --build-arg COOL_ID=myid -t myjava .

If you missed passing COOL_ID you will be notified.

enter image description here

Upvotes: 3

Related Questions