gpt4 book ai didi

database - docker-compose:将变量传递给 tomcat 上下文

转载 作者:行者123 更新时间:2023-11-28 22:48:08 26 4
gpt4 key购买 nike

我有以下问题:

我想在我的 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'

webapp 应用程序是使用 tomcat 部署的。我想通过以下方式在 context.xml 文件中使用变量 WEBAPPDB:

<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}"
/>

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

最佳答案

以这种方式工作:

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>

关于database - docker-compose:将变量传递给 tomcat 上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50756525/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com