gpt4 book ai didi

java - 来自 docker-compose 环境的 catalina.sh 系统变量

转载 作者:行者123 更新时间:2023-11-28 21:57:37 27 4
gpt4 key购买 nike

我有以下问题。我想从描述到 docker-compose.yml 文件的环境变量中设置一些 JAVA_OPTS,以便将它们用于我的 context.xml of tomcat

例子:

docker-compose.yml

services:
myapp:
build: myapp
container_name: myapp
environment:
- DBURL=192.168.7.17
- DBPORT=3306
- DBUSERNAME=test
- DBPASSWORD=test

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/dbschema"
username="${db_username}"
password="${db_password}"
url="jdbc:mysql://${db_url}:${db_port}/dbschema?useUnicode=true&amp;characterEncoding=UTF-8&amp;characterSetResults=UTF-8"
/>

根据一些 tomcat 指南,我发现必须将系统变量设置到 catalina.sh 脚本或 setenv.sh 脚本中。

所以,我的 catalina.sh 文件包含这一行:

JAVA_OPTS="$JAVA_OPTS -Ddb_url=$DBURL -Ddb_port=$DBPORT -Ddb_username=$DBUSERNAME -Ddb_password=$DBPASSWORD"

但是,执行 ps aux | grep java 来自容器,我有:

root        26  6.5 13.8 5770472 1120204 ?     Sl   09:04   1:38 /usr/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Ddb_url= -Ddb_port= -Ddb_username= -Ddb_password= -Djava.endorsed.dirs=/opt/tomcat/endorsed -classpath /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/opt/tomcat -Dcatalina.home=/opt/tomcat -Djava.io.tmpdir=/opt/tomcat/temp org.apache.catalina.startup.Bootstrap start

应用的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/

EXPOSE 8282

CMD service tomcat start && touch /opt/tomcat/logs/myapp.log && tail -f /opt/tomcat/logs/myapp.log

更新 2

我试过这个:

docker-compose.yml

services:
myapp:
build:
context: myapp
args:
db_url: 192.168.7.17
db_port: 3306
db_username: eudata
db_password: eudata
container_name: myapp

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/

ARG db_url
ARG db_port
ARG db_username
ARG db_password

ENV db_url ${db_url}
ENV db_port ${db_port}
ENV db_username ${db_username}
ENV db_password ${db_password}

EXPOSE 8282

CMD service tomcat start && touch /opt/tomcat/logs/myapp.log && tail -f /opt/tomcat/logs/myapp.log

我已将以下 setenv.sh 文件添加到 tomcat/bin 文件夹中:

echo "I'm in setenv.sh"

JAVA_OPTS="$JAVA_OPTS -Ddb_url=$db_url -Ddb_port=$db_port -Ddb_username=$db_username -Ddb_password=$db_password"

export JAVA_OPTS

但什么也没有。如果我尝试像这样将环境变量打印到 setenv.sh 中:

echo "Environment variables"

env

JAVA_OPTS="$JAVA_OPTS -Ddb_url=$db_url -Ddb_port=$db_port -Ddb_username=$db_username -Ddb_password=$db_password"

export JAVA_OPTS

我看不到任何人设置的变量(db_url、db_port、db_username 或 db_password)。

更新 3

我是这样走的:

我有一个像这样的 docker-compse.yml:

services:
myapp:
build: myapp
container_name: myapp
environment:
- JAVA_OPTS=-Djdbc.url=192.168.7.17 -Djdbc.port=3306 -Djdbc.username=test -Djdbc.password=test

我的server.xml:

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

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="${jdbc.username}"
password="${jdbc.password}"
url="jdbc:mysql://${jdbc.url}:${jdbc.port}/interaction_server?useUnicode=true&amp;characterEncoding=UTF-8&amp;characterSetResults=UTF-8"
</GlobalNamingResources>

context.xml:

<Context>
<ResourceLink
name="jdbc/myapp"
global="jdbc/myapp"

auth="Container"

type="javax.sql.DataSource"
/>
</Context>

但是我有以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myAppServiceImpl': Invocation of init method failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Cannot open connection
....
Caused by: java.sql.SQLException: Illegal connection port value '${jdbc.port}'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)....

谁能帮帮我?谢谢。

最佳答案

以这种方式工作:

docker-compose.yml:

services:
myapp:
build: myapp
container_name: myapp
environment:
- JAVA_OPTS= -Ddb.url=192.168.7.17 -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/myapp.log && tail -f /opt/tomcat/logs/myapp.log

server.xml:

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

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="${db.username}"
password="${db.password}"
url="jdbc:mysql://${db.url}:${db.port}/myapp?useUnicode=true&amp;characterEncoding=UTF-8&amp;characterSetResults=UTF-8"
</GlobalNamingResources>

关于java - 来自 docker-compose 环境的 catalina.sh 系统变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51610364/

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