[英]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;
}
}
map = Main-Map
spring.profiles.active=${profile}
host = Development-host
如您所見,我正在從外部設置 application.properties 中的配置文件值。 這就是我試圖通過 docker 注入的內容
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 build -t demo:latest .
docker run -p 8083:8080 demo:latest
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 中。
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.