1

I have the following problem:

I want to define an environment variable into my docker-compose.yml file as follow:

services:
  nginx:
    image: nginx:1.13
    container_name: nginx
    restart: always
    ports: 
        - "80:80"
        - "9090:9090"
    volumes: 
        - ./nginx/conf.d:/etc/nginx/conf.d
        - ./nginx/html:/usr/share/nginx/html

  webapp:
    build: WebApp
    container_name: webapp
    environment:
      - WEBAPPDB=jdbc:mysql://192.168.101.129:3306/webapp?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8
    expose: 
        - "8080"
    depends_on:
        - nginx
version: '2'

the webapp application is deployed using tomcat. I would like to use the variable WEBAPPDB into the context.xml file in the following way:

<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/webapp"
        username="username"
        password="password"
        url="${WEBAPPDB}"
    />

How can I do this? thanks for you help.

br1
  • 357
  • 1
  • 5
  • 19

2 Answers2

2

Working in this way:

docker-compose.yml:

services:
  webapp:
    build: webapp
    container_name: webapp
    environment:
      - JAVA_OPTS= -Ddb.url=192.168.101.129 -Ddb.port=3306 -Ddb.username=test -Ddb.password=test

Dockerfile:

FROM bp91/ubuntu16.04-tomcat7-java8

COPY webapps /tmp/webapps/

ADD tomcat/bin /opt/tomcat/bin/

RUN chmod 775 /opt/tomcat/bin/catalina.sh

RUN chown root:root /opt/tomcat/bin/catalina.sh

RUN cp -r /tmp/webapps/* /opt/tomcat/webapps/

ENV JAVA_OPTS ""

EXPOSE 8282

CMD sh /opt/tomcat/bin/catalina.sh $JAVA_OPTS && touch /opt/tomcat/logs/webapp.log && tail -f /opt/tomcat/logs/webapp.log

server.xml:

<GlobalNamingResources>
   <Resource
      auth="Container"
      driverClassName="com.mysql.jdbc.Driver"
      type="javax.sql.DataSource"
      global="jdbc/webapp"

      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/webapp"
      username="${db.username}"
      password="${db.password}"
      url="jdbc:mysql://${db.url}:${db.port}/webapp?useUnicode=true&amp;characterEncoding=UTF-8&amp;characterSetResults=UTF-8"
</GlobalNamingResources>
br1
  • 357
  • 1
  • 5
  • 19
  • Perfect. Note, that JAVA_OPTS also works in the official tomcat docker image (https://hub.docker.com/_/tomcat) just like that. So no need to create a Dockerfile just for that. – nharrer Oct 31 '20 at 12:38
0

According to the documentation Tomcat Configuration Reference

Tomcat configuration files are formatted as schemaless XML; elements and attributes are case-sensitive. Apache Ant-style variable substitution is supported; a system property with the name propname may be used in a configuration file using the syntax ${propname}. All system properties are available including those set using the -D syntax, those automatically made available by the JVM and those configured in the $CATALINA_BASE/conf/catalina.properties file.

In order to make it work the WEBAPPDB variable in context.xml should be available as a system property. You can set the system properties in several ways (see Tomcat 7 - where do I set 'system properties'? ). In your case you could set the environment variable JAVA_OPTS in the docker-compose file:

environment:
  - JAVA_OPTS=-DWEBAPPDB=jdbc:mysql://192.168.101.129:3306/webapp?useUnicode=true&amp;characterEncoding=UTF-8&amp;characterSetResults=UTF-8
b0gusb
  • 4,283
  • 2
  • 14
  • 33
  • @b0fusb I've tried this way, but it doesn't work. It's not a problem of environment variables (if I enter into the container, echo $JAVA_OPTS prints me these variables). But it seems that the file context.xml doesn't see environment variables – br1 Jul 20 '18 at 12:31