use*_*987 6 java spring docker dockerfile
我正在尝试 dockerize 我的 Spring 应用程序。
问题: 我无法从 docker 容器中获取 Spring 应用程序中的环境变量。
Spring 配置(2 个选项,单独尝试)
<bean class="java.net.URI" id="dbUrl">
<constructor-arg value="#{systemProperties['JDBC_CONNECTION_STRING']}"/>
</bean>
<bean class="java.net.URI" id="dbUrl">
<constructor-arg value="#{systemEnvironment['JDBC_CONNECTION_STRING']}"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
也试过java
URI dbUrl = URI.create(System.getProperty("JDBC_CONNECTION_STRING"));
Run Code Online (Sandbox Code Playgroud)
我的码头工人配置。使用docker-compose build并docker-compose up每次更新值。
docker-compose.yml
app:
build: .
command: catalina.sh run
ports:
- "8888:8080"
links:
- postgres
volumes:
- /usr/bin
postgres:
image: postgres:9.5
ports:
- "5432"
volumes:
- /var/lib/postgresql/data
Run Code Online (Sandbox Code Playgroud)
文件
FROM tomcat:jre8
ENV JDBC_CONNECTION_STRING 'postgres://postgres:password111@postgres:5432/mydb'
RUN ["rm", "-fr", "/usr/local/tomcat/webapps/ROOT"]
RUN apt-get update && apt-get install -y net-tools postgresql-client
COPY ./target/myapp.war /usr/local/tomcat/webapps/ROOT.war
CMD ["catalina.sh", "run"]
Run Code Online (Sandbox Code Playgroud)
一旦我连接到容器的 bash,set命令就不会显示我的变量。但echo $JDBC_CONNECTION_STRING显示价值。
小智 2
在java代码中,您使用的是java系统属性,但不是系统环境变量。为了将系统属性传递给java进程,您需要在运行命令中指定-Dkey=value。
因此,如果这是tomcat,您可以在 $JAVA_OPTS="... -DJDBC_CONNECTION_STRING=$JDBC_CONNECTION_STRING" 中设置
| 归档时间: |
|
| 查看次数: |
5718 次 |
| 最近记录: |