Rannick
Rannick

Reputation: 596

Spring Templates in Docker Container

So I've been trying to dockerify my spring-mvc 4 app using the java:8 base image:

I'm having trouble on the following line when running in docker, but outside of docker everything works fine! It has to do with finding the template files for my project:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/pages/**").addResourceLocations("classpath:/pages/");
}

As a note, I've tried running with the template files inside of a jarfile and outside of a jarfile. The url debugging code has shown that it's finding the right files, but when it tries to use them, I get the following exception:

engine_1 | jvm 1    |java.io.FileNotFoundException: class path resource [pages/saga-index.html] cannot be resolved in the file system for resolving its last-modified timestamp
engine_1 | jvm 1    |   at org.springframework.core.io.AbstractResource.lastModified(AbstractResource.java:155)
engine_1 | jvm 1    |   at org.springframework.core.io.AbstractFileResolvingResource.lastModified(AbstractFileResolvingResource.java:169)
engine_1 | jvm 1    |   at org.springframework.web.servlet.resource.ResourceHttpRequestHandler.handleRequest(ResourceHttpRequestHandler.java:229)
engine_1 | jvm 1    |   at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:51)
engine_1 | jvm 1    |   at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
engine_1 | jvm 1    |   at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
engine_1 | jvm 1    |   at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
engine_1 | jvm 1    |   at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
engine_1 | jvm 1    |   at javax.servlet.http.HttpServlet.service(HttpServlet.java:735)
engine_1 | jvm 1    |   at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
engine_1 | jvm 1    |   at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
engine_1 | jvm 1    |   at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:684)
engine_1 | jvm 1    |   at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:503)
engine_1 | jvm 1    |   at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)

How can i get through this exception? Why am I not able to get a timestamp?

(More stats)

$ docker exec siteconfiguration_engine_1 java -version
openjdk version "1.8.0_40-internal"
OpenJDK Runtime Environment (build 1.8.0_40-internal-b27)
OpenJDK 64-Bit Server VM (build 25.40-b25, mixed mode)

$ docker exec siteconfiguration_engine_1 df -h
Filesystem      Size  Used Avail Use% Mounted on
none             19G  2.5G   15G  15% /
tmpfs          1004M     0 1004M   0% /dev
shm              64M     0   64M   0% /dev/shm
/dev/sda1        19G  2.5G   15G  15% /etc/hosts

$ docker exec siteconfiguration_engine_1 ls -l /decision-engine/etc/pages
total 4
-rw-r--r-- 1 root root 809 Jan  1  1970 saga-index.html

Upvotes: 3

Views: 1266

Answers (3)

blurryrunner
blurryrunner

Reputation: 78

If you are following the Spring Boot with Docker guide, the comment about needing to touch the jar is no longer true if you are using the Spotify docker plugin > v0.2.11. The workaround listed in there and in other answers can be discarded.

Source: https://github.com/spotify/docker-maven-plugin/issues/58

Upvotes: 1

Harry Lime
Harry Lime

Reputation: 29576

I used a workaround that is mentioned in the spring-boot-docker documentation (in the 2nd note)

Adding

RUN touch my.jar

to your Dockerfile will solve the problem.

Upvotes: 0

Rannick
Rannick

Reputation: 596

It appears that, by default, docker drops files as being never modified into an image. Spring has put in a sanity check on its templates to see if they've ever been updated. Executing the following commands resolved my problem, and now I'm left thinking that one of these codebases needs a bug report against it.

$ docker exec siteconfiguration_engine_1 bash -c 'touch -m /decision-engine/etc/pages/saga-index.html'
$ docker exec siteconfiguration_engine_1 ls -l /decision-engine/etc/pages
total 4
-rw-r--r-- 1 root root 810 Mar 30 02:39 saga-index.html

Here are the bugs that @Rannick created:

Upvotes: 2

Related Questions