大网数据广告活动
服务器运维问答

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

· 52CE 我爱测

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"); Stri…

问题原文

原帖链接 · PHP Avenger · CC BY-SA 3.0

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)

已采纳解答

原帖链接 · PHP Avenger · CC BY-SA 3.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-10-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。