Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass System property to docker containers?

Tags:

java

docker

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
like image 638
feco Avatar asked Oct 29 '15 07:10

feco


People also ask

How do I pass environment variables to Docker containers?

With a Command Line Argument The command used to launch Docker containers, docker run , accepts ENV variables as arguments. Simply run it with the -e flag, shorthand for --env , and pass in the key=value pair: sudo docker run -e POSTGRES_USER='postgres' -e POSTGRES_PASSWORD='password' ...

Do Docker containers inherit environment variables?

Using docker-compose , you can inherit env variables in docker-compose. yml and subsequently any Dockerfile(s) called by docker-compose to build images. This is useful when the Dockerfile RUN command should execute commands specific to the environment.

How do I pass an environment variable from Docker Compose to Dockerfile?

Pass variables into Dockerfile through Docker Compose during build. If you want to pass variables through the docker-compose process into any of the Dockerfiles present within docker-compose. yml , use the --build-arg parameter for each argument to flow into all of the Dockerfiles.

How do I set an environment variable on an existing container?

How to set the Environment variable in the container? To set an environment variable you should use flag -e while using docker run or docker-compose command. Environment file - If the environment variable is not overridden by docker-compose.


Video Answer


2 Answers

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
like image 160
michaelbahr Avatar answered Sep 19 '22 19:09

michaelbahr


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!

like image 20
Isidro.rn Avatar answered Sep 21 '22 19:09

Isidro.rn