我有一个带有以下文件的spring boot项目:
我的
Dockerfile-dev
包含以下setp:FROM gradle:5.2.1-jre11 AS BUILDSTAGE
USER root
WORKDIR /output
COPY . .
RUN gradle application:build -Dspring.profiles.active=dev --no-daemon
RUN find /output/application/build/libs
FROM openjdk:11-jre
WORKDIR /app
COPY application/wait-for-it.sh ./wait-for-it.sh
COPY --from=BUILDSTAGE /output/application/build/libs/application-*.jar ./application.jar
RUN chmod +x wait-for-it.sh
ENTRYPOINT ["./wait-for-it.sh", "db:3306", "--", "java", "-jar", "application.jar"]
请不要对
wait-for-it.sh
感到困惑,这是一个简单的shell脚本,它可以确保在启动数据库后启动Spring Boot。如您所见,我是使用
-Dspring.profiles.active=dev
构建的,因此,根据我的理解,这是将 Activity 的 Spring 配置文件设置为dev
,这应确保application-dev.yml
配置得到使用,而不是application.yml
。我的
build.gradle
包含:bootRun {
systemProperties = System.properties
}
我的
docker-compose
文件正在使用Dockerfile-dev
:version: "3"
services:
db:
image: mysql:8.0.18
environment:
MYSQL_DATABASE: "test"
MYSQL_USER: "test"
MYSQL_PASSWORD: "test"
MYSQL_ROOT_PASSWORD: "mysql"
MYSQL_ROOT_HOST: "%"
ports:
- "3306:3306"
app:
depends_on:
- db
build:
context: ..
dockerfile: application/Dockerfile-dev
ports:
- "8080:8080"
- "5005:5005"
当我执行
docker-compose -f application/docker-compose-dev.yml up --force-recreate --build -V --remove-orphans
时,得到以下输出:Building app
Step 1/12 : FROM gradle:5.2.1-jre11 AS BUILDSTAGE
---> d57eadc2e4d3
Step 2/12 : USER root
---> Using cache
---> 72aa645c307e
Step 3/12 : WORKDIR /output
---> Using cache
---> 327fd32c6c3d
Step 4/12 : COPY . .
---> 055d0c0c09e9
Step 5/12 : RUN gradle application:build -Dspring.profiles.active=dev --no-daemon
---> Running in c2a23d7fc2b3
因此,这似乎很好。但是稍后在spring应用程序启动时的日志中:
app_1 |
app_1 | . ____ _ __ _ _
app_1 | /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
app_1 | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
app_1 | \\/ ___)| |_)| | | | | || (_| | ) ) ) )
app_1 | ' |____| .__|_| |_|_| |_\__, | / / / /
app_1 | =========|_|==============|___/=/_/_/_/
app_1 | :: Spring Boot :: (v2.1.4.RELEASE)
app_1 |
app_1 | 2019-11-27 09:47:15,388 INFO [main] [d.f.backend.ApplicationKt] - Starting ApplicationKt on 5f5fc7f3dddf with PID 1 (/app/application.jar started by root in /app)
app_1 | 2019-11-27 09:47:15,403 INFO [main] [d.f.backend.ApplicationKt] - No active profile set, falling back to default profiles: default
如您所见
No active profile set
,所以它将使用默认值application.yml
。所以似乎我有一个误会/配置错误,如何在我的设置中设置 Activity 的 Spring 轮廓。我该如何设置?
最佳答案
您在构建应用程序时设置配置文件,而在运行应用程序时(通过入口点)需要设置配置文件:
ENTRYPOINT ["./wait-for-it.sh", "db:3306", "--", "java", "-Dspring.profiles.active=dev", "-jar", "application.jar"
关于spring - 在docker中使用gradle设置事件的 Spring 配置文件没有任何效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59087151/