Thymeleaf fails to resolve templates when running inside Docker container

荒凉一梦 提交于 2021-01-29 17:38:05

问题


I have a standard project structure for SpringBoot apps:

When I build and run my app like this:

mvn clean install
mvn spring-boot:run

everything works.

When I try to run inside docker container specified by docker-compose

file:
version: "3.1"
services:
  app:
    container_name: thdnes-app
    restart: always
    build:
      context: ./
      dockerfile: Dockerfile
    ports:
      - "8080:8080"

and Dockerfile:

FROM openjdk:11-jdk
VOLUME /tmp
COPY /target/thdnes-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","app.jar"]

SpringBoot starts fine (judging by console output, no errors).

But when I navigate to http://localhost:8080/login I get an error:

thdnes-app | 2019-07-26 13:32:11.361 ERROR 1 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "/login.html": Error resolving template [/login.html], template might not exist or might not be accessible by any of the configured Template Resolvers
thdnes-app | 
thdnes-app | org.thymeleaf.exceptions.TemplateInputException: Error resolving template [/login.html], template might not exist or might not be accessible by any of the configured Template Resolvers
thdnes-app |    at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
thdnes-app |    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
thdnes-app |    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) ~[thymeleaf-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
thdnes-app |    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) ~[thymeleaf-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
thdnes-app |    at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362) ~[thymeleaf-spring5-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
thdnes-app |    at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189) ~[thymeleaf-spring5-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
thdnes-app |    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1370) ~[spring-webmvc-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
thdnes-app |    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1116) ~[spring-webmvc-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
thdnes-app |    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1055) ~[spring-webmvc-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
thdnes-app |    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
thdnes-app |    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
thdnes-app |    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) ~[spring-webmvc-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
thdnes-app |    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.17.jar!/:9.0.17]
thdnes-app |    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
thdnes-app |    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.17.jar!/:9.0.17]
...

As tested without Docker involved, the template exists and is packed in a jar, but why it fails inside a container is my question.


回答1:


I was experiencing the same problem, when started locally via maven with

mvn spring-boot:run

everything was working just fine, but when when i ran it from a docker container, thymeleaf was not finding templates.

The solution was to remove / from the beginning of the view names, like in the example below:

BEFORE

    public String findOwners(Model model) {
        model.addAttribute("owner", new Owner());
        return "/owners/findOwners";
    }

AFTER

    public String findOwners(Model model) {
        model.addAttribute("owner", new Owner());
        return "owners/findOwners";
    }


来源:https://stackoverflow.com/questions/57221353/thymeleaf-fails-to-resolve-templates-when-running-inside-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!