'How to pass System property to docker containers?

So I know you can pass Environment variables to a docker container using -e like:

docker run -it -e "var=var1" myDockerImage

But I need to pass a System Property to a docker container, because this is how I run my JAR:

java -Denvironment=dev -jar myjar.jar 

So how can I pass a -D System property in Docker? Like:

docker run -it {INSERT Denvironment here} myDockerImage


Solution 1:[1]

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!

Solution 2:[2]

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"

Solution 3:[3]

Have a start.sh file, e.g.:

#!/usr/bin/env sh
exec java -Djava.security.egd=file:/dev/./urandom $* -jar /app.jar

In your Dockerfile:

...
COPY start.sh /start.sh
RUN chmod a+rx /start.sh
ENTRYPOINT ["/start.sh"]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jmizv
Solution 2 DevNG
Solution 3 naimdjon