docker-compose environment variables to tomcat context.xml

2.8k Views Asked by At

I have the following problem: I want to define, in my docker-compose file, some environments variable for my db connection.

services:
   myapp:
      build: SessionHandler
      container_name: sessionhandler
      environment:
         - JAVA_OPTS=-DdbUrl=10.5.0.1 -DdbPort=3306 -DdbUsername=root -DdbPassword=root
ports: 
    - "8080:8080"
volumes:
    - ./myapp/tomcat/conf/:/opt/tomcat/conf/

I want to use these variables into my context.xml, in this way:

<Context>
<Resource
    auth="Container" 
    driverClassName="com.mysql.jdbc.Driver"
    type="javax.sql.DataSource"

    initialSize="0"
    maxActive="10"
    maxIdle="5"
    maxWait="5000"
    minIdle="0"
    timeBetweenEvictionRunsMillis="34000"
    minEvictableIdleTimeMillis="55000"

    testOnBorrow="true"
    testWhileIdle="true"
    testOnReturn="false"
    validationQuery="SELECT 1 FROM dual"
    validationInterval="30000"
    removeAbandoned="true"
    removeAbandonedTimeout="10"

    name="jdbc/myapp" 
    username="${dbUsername}"
    password="${dbPassword}"
    url="jdbc:mysql://${dbUrl}:${dbPort}/myapp?useUnicode=true&amp;characterEncoding=UTF-8&amp;characterSetResults=UTF-8"
/>
</Context>

But it doesn't work. (Browser gives me a 500)

If I explicit variables into the context.xml, in this way:

    <Context>
<Resource
    auth="Container" 
    driverClassName="com.mysql.jdbc.Driver"
    type="javax.sql.DataSource"

    initialSize="0"
    maxActive="10"
    maxIdle="5"
    maxWait="5000"
    minIdle="0"
    timeBetweenEvictionRunsMillis="34000"
    minEvictableIdleTimeMillis="55000"

    testOnBorrow="true"
    testWhileIdle="true"
    testOnReturn="false"
    validationQuery="SELECT 1 FROM dual"
    validationInterval="30000"
    removeAbandoned="true"
    removeAbandonedTimeout="10"

    name="jdbc/myapp" 
    username="root"
    password="root"
    url="jdbc:mysql://10.5.0.1:3306/myapp?useUnicode=true&amp;characterEncoding=UTF-8&amp;characterSetResults=UTF-8"
/>
</Context>

everything works. Can anyone help me? Really thanks.

UPDATE

I've inserted into catalina.properties file following system variables:

dbUrl=10.5.0.1 
dbPort=3306 
dbUsername=root 
dbPassword=root

and the context.xml

<Resource
    auth="Container" 
    driverClassName="com.mysql.jdbc.Driver"
    type="javax.sql.DataSource"

    initialSize="0"
    maxActive="10"
    maxIdle="5"
    maxWait="5000"
    minIdle="0"
    timeBetweenEvictionRunsMillis="34000"
    minEvictableIdleTimeMillis="55000"

    testOnBorrow="true"
    testWhileIdle="true"
    testOnReturn="false"
    validationQuery="SELECT 1 FROM dual"
    validationInterval="30000"
    removeAbandoned="true"
    removeAbandonedTimeout="10"

    name="jdbc/myapp" 
    username="${dbUsername}"
    password="${dbPassword}"
    url="jdbc:mysql://${dbUrl}:${dbPort}/myapp?useUnicode=true&amp;characterEncoding=UTF-8&amp;characterSetResults=UTF-8"
/>

And works. So I tried to define them empty, in order to define them into the docker-compose.yml:

dbUrl= 
dbPort= 
dbUsername= 
dbPassword=

But it doesn't work. Any help please?

1

There are 1 best solutions below

2
atline On

Try next, directly add these parameters as container environment variable, I think if no java -D to specify these environments, environment variable in container could also be passed to java programs.

services:
  myapp:
    build: SessionHandler
    container_name: sessionhandler
    environment:
      - dbUrl=10.5.0.1
      - dbPort=3306
      - dbUsername=root
      - dbPassword=root