【问题标题】:Docker cannot find Resource on classpathDocker 在类路径上找不到资源
【发布时间】:2023-03-13 09:03:01
【问题描述】:

我有一个简单的 gradle spring boot java 应用程序,我正在尝试使用 spring boot 分析从“application.properties”和“application-dev.properties”获取一些属性值。它工作正常,当我尝试在本地机器上运行应用程序时,spring boot 配置文件正在加载,但是当我尝试在 docker 上运行相同的应用程序时,突然弹出错误消息,提示应用程序无法在类路径。

下面是项目结构

在 App.Config 类中,我有以下代码。如您所见,我正在尝试从 application.properties 文件中获取属性值。

@Component
@PropertySource("classpath:application.properties")
public class AppConfig {

@Value("${host}")
private String host;
@Value("${map}")
private String map;

public String getHost() {
    return host;
}

public void setHost(String host) {
    this.host = host;
}

public String getMap() {
    return map;
}

public void setMap(String map) {
    this.map = map;
}
}

application.properties 包含以下代码。

map = Main-Map

spring.profiles.active=${profile}

application-dev.properties 包含以下代码

host = Development-host

如您所见,我正在从外部在 application.properties 中设置配置文件值。这就是我试图通过 docker 注入的内容

Dockerfile 包含以下代码

FROM java:8

VOLUME /tmp

ENV tom=dev

ADD build/libs/demo-0.0.1-SNAPSHOT.jar /app/app.jar

ADD build/resources/main/application.properties /app/application.properties

ADD build/resources/main/application-dev.properties /app/application-dev.properties

WORKDIR /app

ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","- 
Dprofile=${tom}","-jar","app.jar", "- 
-spring.config.location=/app/application.properties, /app/application- 
dev.properties"]

我使用以下命令构建 docker 映像

docker build -t demo:latest .

我使用以下命令运行 docker

docker run -p 8083:8080 demo:latest

当我运行 docker run 命令时,出现异常。

2019-12-12 07:09:50.405  INFO 1 --- [           main] com.example.demo.DemoApplication         : 
Starting DemoApplication on ed7cb11b8a34 with PID 1 (/app/app.jar started by root in /app)
2019-12-12 07:09:50.407  INFO 1 --- [           main] com.example.demo.DemoApplication         : The 
following profiles are active: dev
2019-12-12 07:09:50.603  WARN 1 --- [           main] ConfigServletWebServerApplicationContext : 
Exception encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class 
[com.example.demo.DemoApplication]; nested exception is java.io.FileNotFoundException: class path 
resource [application.properties] cannot be opened because it does not exist
2019-12-12 07:09:50.712 ERROR 1 --- [           main] o.s.boot.SpringApplication               : 
Application run failed

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [   com.example.demo.DemoApplication]; nested exception is java.io.FileNotFoundException: class path 
resource [ application.properties] cannot be opened because it does not exist
    at 

任何帮助将不胜感激。

提前致谢。

【问题讨论】:

标签: java spring spring-boot dockerfile


【解决方案1】:

看来你不是在构建 Fat Jar。您可以为此使用spring-boot-maven-plugin

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.0.1.RELEASE</version>
</plugin>

然后更改您的Dockerfile 喜欢:

FROM java:8
VOLUME /tmp
ENV tom=dev
COPY build/libs/demo-0.0.1-SNAPSHOT.jar /app/app.jar
WORKDIR /app
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Dprofile=${tom}","-jar","app.jar"]

【讨论】:

    【解决方案2】:

    它与 docker 无关。您是否尝试过构建 jar 文件并通过 java -jar 运行它。

    ADD build/resources/main/application.properties /app/application.properties
    
    ADD build/resources/main/application-dev.properties /app/application-dev.properties
    

    这些也是不必要的。当您将工件创建为 fat/uber/Shadow jar 时,您会将属性打包在 jar 中。

    如何构建 fat/uber/Shadow Jar (Spring boot Gradle)

    https://plugins.gradle.org/plugin/org.springframework.boot (Gradle: Build 'fat jar' with Spring Boot Dependencies)

    替代方案

    https://imperceptiblethoughts.com/shadow/ (https://github.com/johnrengelman/shadow)

    【讨论】:

    • 感谢您的回复。这对我帮助很大。
    猜你喜欢
    • 2016-06-15
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 2018-07-18
    • 1970-01-01
    相关资源
    最近更新 更多