Docker在类路径上找不到资源

时间:2019-12-12 07:40:51

标签: java spring spring-boot dockerfile

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

下面是项目结构

{{3}}

在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 

任何帮助将不胜感激。

谢谢。

2 个答案:

答案 0 :(得分: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"]

答案 1 :(得分:1)

与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中。

如何制作胖子/超级/阴影罐(春季靴子摇篮)

https://plugins.gradle.org/plugin/org.springframework.bootGradle: Build 'fat jar' with Spring Boot Dependencies

替代

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