r/javahelp • • May 04 '19

Solved Java (Spring Boot) and Docker done properly?

When containerising an application, all tutorials on Java seem to want you to build a jar locally and copy it from target into a container in the Dockerfile. However, this contradicts the whole idea behind using Docker because building on my machine will not necessarily be the same as building on a colleague’s machine.

I can’t find a tutorial that describes using Maven via Docker to build the jar and moving the result to a Java container i.e. via staged builds. This would ensure consistency across machines and follows the whole Docker ethos.

Can anyone recommend a way to do this or point me to a good tutorial?

30 Upvotes

19 comments sorted by

View all comments

5

u/snotsnot May 04 '19

https://github.com/tonsV2/whoami - The master branch contains a Dockerfile which in my opinion reflects best practice. There are various other branches which might be of interest too. Please let me know if there's any feedback.

2

u/probable-maybe May 04 '19

Thank you, that’s perfect!

3

u/thecuseisloose Do you even Java, bro? May 04 '19 edited May 04 '19

One thing that can be beneficial with dependency management is having the dependencies downloaded in their own stage. This will allow docker layer caching, and dependencies won’t be downloaded every build unless the pom changes. To do this with maven, first copy only your pom to the docker image, then run mvn verify —-fail-never. After this, copy your remaining code to the image and package it. Subsequent builds will be faster as long as your dependencies don’t change

Idk what the equivalent command is in gradle but I’m sure there is one