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
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
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