Docker conatiner: Get passed environment varaible in tomcat spring mvc application

dockerenvironment-variablestomcat

I am trying to deploy a SpringMVC application within a Docker container on top to tomcat8.

My problem is This SpringMVC application receive parameters using following code

String username = System.getProperty("db.username");
String password = System.getProperty("db.password");

I am running Docker container with the following command

docker run -e "db.username=root" -e "db.password=123" -v /home/ubuntu/code:/usr/local/tomcat/webapps -i -t -p 80:8080 tomcat

It starts the tomcat conatiner and try to deploy the application, but it does not work and throw application specifc errors username is empty and password is empty.

System.getProperty is not able to receive the passed environment varaibles.

Important: Please note I cannot change the code, I can only modify the docker related things.

I think there should be a way to transform passed environment variables into Java property (in order for System.getProperties to work)

Best Answer

I solved my problem with the following command, but I will still wait for a better answer to accept.

docker run -v /home/ubuntu/code:/usr/local/tomcat/webapps -e JAVA_OPTS="-Ddb.username=root -Ddb.password=abc123" -it -p 80:8080 tomcat

In above command I am trying to run a tomcat container, I have mounted host directory onto container's tomcat webapps directory.

Then I am passing an environment variable JAVA_OPTS which will set Java properties that I will read in my Java code using System.getProperty("db.username")

Related Topic