繁体   English   中英

Docker 在类路径上找不到资源

[英]Docker cannot find Resource on classpath

我有一个简单的 gradle spring boot java 应用程序,我正在尝试使用 spring boot profiling 从“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 

任何帮助,将不胜感激。

提前致谢。

看起来你不是在建造 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"]

它与码头工人无关。 您是否尝试过构建 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 )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM