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

1

u/aenigmaclamo Extreme Brewer May 04 '19

I don't think it is necessarily best practice to perform the build within docker and I think it depends on why you use docker.

We use docker during development as well as deployment. In that case, building from docker becomes extremely slow because you lose the Gradle cache. Some people do some things that I find insane here, like volume mounting the caches or the local filesystem to speed up builds, but I'd rather just perform the build locally.

We also use a Docker based Gitlab runner which has Java that allows you to run Gradle and such for our "deployment" builds.

1

u/rossdrew May 07 '19

It’s pretty common. In fact the Concourse CI is completely reliant on the idea. It’s a little slower but gives greater confidence in automated building and running in different environments. We use it to build and build and run system tests over Java 7,8,9,10 and 11 combinatorially agains H2, MySQL, Oracle and Postgres. There’s no neater way that I know of.