Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerfile CMD not accepting variables for substitution

Unable to use variables on the CMD line:

#CMD ["/opt/jdk/bin/java", "-jar", "${ARTIFACTID}-${VERSION}.${PACKAGING}"]
CMD ["/opt/jdk/bin/java", "-jar", "ssltools-domain-LATEST.jar"]

Latest line works, not the first.

Any help or workaround very appreciated

like image 639
MortenB Avatar asked Dec 19 '22 12:12

MortenB


1 Answers

When you write the arguments to CMD (or ENTRYPOINT) as a JSON string, as in...

CMD ["/opt/jdk/bin/java", "-jar", "ssltools-domain-LATEST.jar"]

...the command is executed directly with the exec system call and is not processed by a shell. That means no i/o redirection, no wildcard processing...and no variable expansion. You can fix this in a number of ways:

  • You can just write it as a plain string instead, as in:

      CMD /opt/jdk/bin/java -jar ${ARTIFACTID}-${VERSION}.${PACKAGING}
    

    When the argument is not a JSON construct, it gets passed to sh -c.

  • You can do as suggested by Philip, and pass the arguments to sh -c yourself:

      CMD ["sh", "-c", "/opt/jdk/bin/java -jar ${ARTIFACTID}-${VERSION}.${PACKAGING}"]
    

    Those two options are basically equivalent.

  • A third option is to put everything into a shell script, and then run that:

      COPY start.sh /start.sh
      CMD ["sh", "/start.sh"]
    

    This is especially useful if you need to perform more than a simple command line.

like image 94
larsks Avatar answered Feb 04 '23 07:02

larsks