0

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?

d_vucin91
  • 119
  • 2
  • 12
  • Could you please explain how do you start your application? how do you use `JAVA_OPTS` env variable inside the container? – Bukharov Sergey Jul 19 '18 at 09:16
  • @Bukharov Sergey I have an image with ubuntu and tomcat. Then I have a Dockerfile that copies the .war and invoke a service tomcat start – d_vucin91 Jul 19 '18 at 10:24

1 Answers1

0

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
atline
  • 28,355
  • 16
  • 77
  • 113
  • 1. By mistake, I add `- DdbUrl=10.5.0.1` in answer, have you removed `D`? Update it. 2. I am not familar with tomcat, are you sure `${}` can be replaced by environment variable. – atline Jul 19 '18 at 11:33
  • I did withoud -D, but the error it's the same. In order to your question: http://tomcat.apache.org/tomcat-8.0-doc/config/index.html – d_vucin91 Jul 19 '18 at 16:09