Docker + Tomcat + .properties -- Environment Variables

16,167

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"]
Share:
16,167
Ozzadar
Author by

Ozzadar

BIO: Paul Mauviel, Male, 1989/01/28, Lives in Hanoi, Vietnam, Born in Canada JOB : Co-founder at Klouds. Working docker deploy automation through golang. PLAY: League of Legends, World of Warcraft, Magic the Gathering, Virtual Reality, Super Mario World

Updated on June 04, 2022

Comments

  • Ozzadar
    Ozzadar almost 2 years

    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.

  • Righto
    Righto over 6 years
    How did you edit your context.xml? Was that done using docker commands or directly?
  • Yuriy Kravets
    Yuriy Kravets over 6 years
    @Righto, this answer has nothing to do with editing context.xml.
  • Arashsoft
    Arashsoft about 6 years
    Is it possible to change CATALINA_OPTS from docker run?
  • scudsucker
    scudsucker about 5 years
    @Arashsoft - a bit late to the answer but yes: docker run -e CATALINA_OPTS=-Dkey=value --name [docker.container.name] -p 8080:8080 [serviceName]:[tag]