Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker cannot find Resource on classpath

I have a simple gradle spring boot java application, where I am trying to get some properties value from "application.properties" and "application-dev.properties" using spring boot profiling. It is working fine and spring boot profile is loading when I try to run application on the local machine, but when when I try to run same application on the docker, so suddenly error popsup which says that application is not able to find resources on the classpath.

Below is the project structure

Project structure of the application

In the App.Config class I have the following code. As you can see I am trying to get property value from application.properties file.

@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 contains following code.

map = Main-Map

spring.profiles.active=${profile}

application-dev.properties contains following code

host = Development-host

As you can see I am setting profile value in application.properties from outside. Thats what I am trying to inject through docker

Dockerfile contain following code

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"]

I build docker image using following command

docker build -t demo:latest .

I run docker using following command

docker run -p 8083:8080 demo:latest

When I run docker run command, so comes below exception.

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 

Any help would be appreciated.

Thanks in advance.

like image 916
user3661407 Avatar asked Dec 12 '19 07:12

user3661407


2 Answers

Seems like you aren't building Fat Jar. You can utilize spring-boot-maven-plugin for that.

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

Then change your Dockerfile like:

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"]
like image 93
Sukhpal Singh Avatar answered Oct 20 '22 09:10

Sukhpal Singh


It has nothing to do with the docker. Have you tried building the jar file and running it by java -jar.

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

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

These are unnecessary too. When you create your artifact as fat/uber/Shadow jar, you will have your properties packaged in jar.

How to build fat/uber/Shadow Jar (Spring boot Gradle)

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

Alternative

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

like image 1
so-random-dude Avatar answered Oct 20 '22 07:10

so-random-dude