我正在构建一个简单的 Java 8 应用程序,使用 Vanilla JDBC 连接到 postgres 数据库。在 getConnection()
中的 ConnectionUtil.java
方法中,我将连接凭据保存在以下变量中:
String url = System.getenv("DB_URL");
String username = System.getenv("DB_USERNAME");
String password = System.getenv("DB_PASSWORD");
我的docker-compose.yml
文件如下:
version: "3.9"
services:
jdbc:
build: .
ports:
- 7000:7000
volumes:
- ./logs/log.txt:/logs\log.txt
environment:
- DB_URL
- DB_USERNAME
- DB_PASSWORD
container_name: jdbc
有关其他上下文,这是我的 Dockerfile
:
# Base image with jdk 8 and maven
FROM maven:3.6.3-openjdk-8 as builder
# Copy pom.xml and source-code
COPY pom.xml pom.xml
COPY src/ src/
# Build application
RUN mvn clean package
# As a separate stage, to save on resulting image size, discard everything from previous stages
FROM java:8 as runner
# Base image only needs JRE 8
# Expose port 7000 for our web-app
EXPOSE 7000
# Copy the jar file from previous stage
COPY --from=builder target/jdbc-demo-jar-with-dependencies.jar app.jar
# Run program
ENTRYPOINT [ "java", "-jar", "app.jar" ]
我仍在学习 Docker。我的问题是,我在哪里设置环境变量?
答案 0 :(得分:0)
在您当前的 docker-compose.yaml 中,您声明了环境变量而不初始化它们,只需给它们值。
像这样:
valet install