【问题标题】:docker-compose: passing variable to tomcat contextdocker-compose:将变量传递给tomcat上下文
【发布时间】:2018-06-08 08:30:45
【问题描述】:

我有以下问题:

我想在我的 docker-compose.yml 文件中定义一个环境变量,如下所示:

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'

使用 tomcat 部署 webapp 应用程序。我想通过以下方式将变量 WEBAPPDB 使用到 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/webapp"
        username="username"
        password="password"
        url="${WEBAPPDB}"
    />

我该怎么做?谢谢你的帮助。

【问题讨论】:

    标签: database docker tomcat docker-compose environment-variables


    【解决方案1】:

    以这种方式工作:

    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>
    

    【讨论】:

    • 完美。注意,JAVA_OPTS 也可以在官方的 tomcat docker 镜像 (hub.docker.com/_/tomcat) 中使用。因此,无需为此创建 Dockerfile。
    【解决方案2】:

    根据文档Tomcat Configuration Reference

    Tomcat 配置文件被格式化为无模式 XML;元素 和属性区分大小写。 Apache Ant 风格的变量 支持替换;一个名为 propname 的系统属性 可以使用语法 ${propname} 在配置文件中使用。全部 系统属性可用,包括使用 -D 设置的属性 语法,那些由 JVM 自动提供的和那些 在 $CATALINA_BASE/conf/catalina.properties 文件中配置。

    为了使其正常工作,context.xml 中的 WEBAPPDB 变量应可用作系统属性。您可以通过多种方式设置系统属性(请参阅Tomcat 7 - where do I set 'system properties'? )。在您的情况下,您可以在 docker-compose 文件中设置环境变量 JAVA_OPTS

    environment:
      - JAVA_OPTS=-DWEBAPPDB=jdbc:mysql://192.168.101.129:3306/webapp?useUnicode=true&amp;characterEncoding=UTF-8&amp;characterSetResults=UTF-8
    

    【讨论】:

    • @b0fusb 我试过这种方式,但它不起作用。这不是环境变量的问题(如果我进入容器, echo $JAVA_OPTS 会打印出这些变量)。但似乎文件 context.xml 没有看到环境变量
    猜你喜欢
    • 2023-04-11
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 2020-02-23
    • 2021-11-27
    • 2017-09-18
    • 2018-08-23
    相关资源
    最近更新 更多