Spring boot not finding template when deployed on docker

Viewed 1696

My app works just fine if I run the application on my host using the

mvn spring-boot:run

but when I deploy it on docker, it does not work and I get this error:

SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/store/index", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/store/index", template might not exist or might not be accessible by any of the configured Template Resolvers

but if I go on the url: http:localhost:8080/login which is controlled by spring security, renders the template normally. Seems to be a permission problem but I'm not sure.

here is my Dockerfile:

FROM openjdk
VOLUME /tmp
RUN touch engdevcommerce.jar
COPY target/engdevcommerce.jar engdevcommerce.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/engdevcommerce.jar"]

Solution: Turns out the problem I was having, had nothing to do with the docker deployment. I ran the jar file on my host, and I was getting the same error. The problem was that, where I returned the view url at the controllers methods, I was starting with slash like this: "/.../..." . And spring does not load view with double slash when the application is packed as .jar file. I had to remove the slash character at the beginning of the url every where I returned a ModelAndView and at the th:insert tags too on my html files.

this link helped me a lot : spring-boot-thymeleaf-not-resolving-fragments-after-packaging

2 Answers

Always check the application execution first with 'java -jar your_app_name.jar' command! In general this issue is mostly resolved by checking following 3 points-

  1. Your application.properties should have the entry- spring.thymeleaf.prefix=classpath:/templates/
  2. Your controller should return name of the template without any preceding slash. As the thymeleaf configuration is case sensitive.
  3. Your Project should follow standard spring boot thymeleaf directory structure. If not, then make changes accordingly in your application.properties file.

add your local index directory to dockerfile so it will create /store and copy the index directory to /store then your docker vm will have /store/index with the contents from you local index directory

 ...
 ...
 ADD <local-index-directory> /store
 ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","- 
 jar","/engdevcommerce.jar"]
Related