Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker + Tomcat + .properties -- Environment Variables

I need to deploy a tomcat server setting the MySQL database url inside the

/META-INF/config.properties

into a docker file. The way we're deploying these containers, the IPs can not be hard coded into the program.

Is there a way to pull environment variables from the system within this file? I'd like to do something like this:

mdms.db.url=jdbc:mysql://**${MYSQL_HOST}**/db_mdms?useEncoding=true&characterEncoding=UTF-8&autoReconnect=true
mdms.db.username=root
mdms.db.password=thesecretsauce

I've been searching the internet and have found that I should be able to set some sort of

-DMYSQL_HOST=$MYSQL_HOST

when launching the application but launching tomcat with that flag didn't do the trick and I haven't been able to get it to work.

Another thing I've tried is:

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" value="/WEB-INF/config.properties"/>
        <property name="ignoreResourceNotFound" value="true" />
        <property name="searchSystemEnvironment" value="true" />
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    </bean>

though that seems to not do the trick either.

I also tried both in conjunction with each other.

I'm running on Xubuntu Linux programming in Netbeans (though have figured out how to deploy tomcat without netbeans if that's necessary)

I'm not the application author; nor am I extremely fluent in Tomcat/Java web apps. Nevertheless, this needs to get done and there's a language barrier(or refusal, I haven't figure that out yet) preventing me from getting the author to fix this up for me.

like image 459
Ozzadar Avatar asked Nov 07 '15 13:11

Ozzadar


1 Answers

The way I managed to resolve this problem is setting CATALINA_OPTS in the DockerFile:

ENV CATALINA_OPTS="-Dkey=value"

For example, the whole DockerFile for my app would look like this:

FROM tomcat:8.0-jre8
ENV spring.profiles.active=dev 
ENV CATALINA_OPTS="-Dkey=value"
ADD myWar.war /usr/local/tomcat/webapps/
CMD ["catalina.sh", "run"]
like image 140
Yuriy Kravets Avatar answered Oct 15 '22 13:10

Yuriy Kravets