Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dockerfile – Optional runtime parameter passed to command

I need to pass optional, runtime parameter to a command in Docker.

The idea is that if PARAM env variable is set when docker is being run – it should be passed to java command as --key VALUE , and when runtime parameter is not set – it shoulddn’t pass anything – in particular it shouldn’t pass --key parameter name.

I.e. it should run following command if PARAM env variable is set:
/bin/java -jar artifact.jar --key $PARAM

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

And following if it’s not:
/bin/java -jar artifact.jar

I wanted to use :+ syntax, but it’s resolved during build time, which means it won’t be affected by runtime env variable.

docker build -t test-abc . && docker run -e "PARAM=oooo" test-abc
FROM openjdk:17

ENV PARAM=${PARAM:+"--key $PARAM"}


ENTRYPOINT /bin/java -jar artifact.jar $PARAM

>Solution :

Prepare a script that will correctly handle arguments:

#!/bin/bash
args=()
if [[ -v PARAM ]]; then
   args=(--key "$PARAM")
fi
/bin/java -jar artifact.jar "${args[@]}" "$@"

And add it:

ADD entrypoint.sh /entrypoint.sh
CMD chmod +x /entrypoint.sh
ENTRYPOINT /entrypoint.sh
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading