我有以下问题。 我想从docker-compose.yml文件中描述的环境变量中设置一些JAVA_OPTS,以便将其用于我的 tomcat
的 context.xml 中示例:
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&characterEncoding=UTF-8&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&characterEncoding=UTF-8&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)....
有人可以帮助我吗?谢谢。
答案 0 :(得分:1)
以这种方式工作:
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&characterEncoding=UTF-8&characterSetResults=UTF-8"
</GlobalNamingResources>