How to pass System property to docker containers?

Use the variable you passed into the container on the java command:

docker run -it -e "ENV=dev" myDockerImage
java -Denvironment=$ENV -jar myjar.jar

One more way to do it, if running under Tomcat, is setting your system variables in your Dockerfile using ENV JAVA_OPTS like this:

ENV JAVA_OPTS="-Djavax.net.ssl.trustStore=C:/tomcatDev.jks -D_WS_URL=http://some/url/"

Hope it helps!


One can also use the following start.sh ENTRYPOINT for the Docker container, make sure to use the array syntax, e.g.:

Dockerfile:

...
ENTRYPOINT ["/start.sh"]

The actual start.sh script:

#!/bin/bash
export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"
exec $JAVA_HOME/bin/java -jar myjar.jar $@

Then you can just pass the Java system properties directly to your application as docker run container arguments:

docker run myDockerImage "-Dvar=var1"

Tags:

Docker

Java