Docker无法在类路径中找到资源

7
我有一个简单的Gradle Spring Boot Java应用程序,我正在尝试使用Spring Boot profiling从"application.properties"和"application-dev.properties"获取一些属性值。当我在本地机器上运行应用程序时,它能够正常工作并加载Spring Boot配置文件。但是,当我尝试在Docker上运行相同的应用程序时,突然出现一个错误提示,显示应用程序无法在类路径中找到资源。
以下是项目结构:

Project structure of the application

在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 

我该尝试什么来解决这个问题?

请关注我的 Github 代码库并使用 Docker 文件,这应该可以解决你的问题。https://github.com/naveenkulkarni029/products-api - Naveen Kulkarni
2个回答

2

看起来您没有构建肥的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
它与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使用Spring Boot依赖项构建'fat jar'

另一种选择

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


谢谢您的回复。它确实帮了我很多。 - user3661407

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接