使用 Docker Compose 无法加载 Spring Boot 中的外部配置文件

问题描述 投票:0回答:1

我正在尝试使用“spring.config.import”导入文件。我在文档中看到您可以通过两种方式导入。 1 使用多个文件(configtree),另一个是文件是 .yaml 文件。

我已经看到其他人的问题,但他们正在使用 configtree,我想使用 yaml 文件,因为它可以更轻松地存储多个变量(IMO):

运行应用程序时,它总是显示:

Config data resource 'file [config/backend-secrets.yaml]' via location 'file:./config/secrets.yaml' does not exist

这是我拥有的一个简单的撰写文件

services:
  backend:
    image: ghcr.io/acemint/something:main
    container_name: backend
    environment:
      SPRING_CONFIG_IMPORT: file:./config/backend-secrets.yaml
    secrets:
      - backend_secrets
    volumes:
      - ./config/secrets/:/config
spring spring-boot docker
1个回答
0
投票

解决方案:将 yaml 的路径更改为 Dockerfile 的 WORKDIR

volumes:
  - ./config/secrets/:/app/config

说明: 给出下面的 Dockerfile 的简化版本,我在 /app 上运行 .jar,这自动意味着虽然应用程序似乎试图从 /config 搜索,但它实际上是从 /app/config 搜索。因此该卷应该安装到 /WORKDIR/....

# syntax=docker/dockerfile:1
FROM eclipse-temurin:21-jdk-alpine as build

# Set cwd to /app
WORKDIR /app

# Copy the executable JAR file from the build stage
COPY --from=build /app/target/x-admin-0.0.1-SNAPSHOT*.jar /app.jar

# Define the default command to run the Spring Boot application
CMD ["java", "-Dserver.port=${PORT}", "-Dspring.profiles.active=production", "-jar", "/app.jar"]
© www.soinside.com 2019 - 2024. All rights reserved.