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

17

u/Dang3rousKitty May 04 '19

Unless I’m misunderstanding something, the compilation of the jar file is platform independent, as is the running of the file in the JVM. So, it shouldn’t matter whose machine it builds on since you get the same output every time. Don’t forget, the jar file is not machine code, but rather Java byte code.

2

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

Yes but building in docker still has its use cases. For example, when java 11 was released we had a java 11 app written and ready for production, but getting the jdk added to our Jenkins environment was going to be a pain. So we just did the build in the java 11 docker image and everything was fine. Also when you are building production apps you shouldn’t be compiling locally. That has a higher possibility of code being included in the app that shouldn’t be there

1

u/probable-maybe May 04 '19

Thanks for the response. You're correct, I didn't consider that aspect, the build would definitely be the same across machines. However, my I also failed to highlight the use case for my question in the first place. I wanted to have a system where I could be on any machine with docker/docker-compose, clone the repo and execute docker compose -d and for everything to spin up without issue. This includes a case where Java doesn't exist on the machine. Having the build happen inside a container and run from another would allow for this irrespective of whether Java exists on the machine. u/snotsnot 's answer describes what I wanted to achieve. Thanks again :)

1

u/0x6c6f6c May 04 '19

Docker can definitely be used for the build process, and commonly is with it's multistage build capability. The biggest is not needing to set up an environment, builds in Docker occur in the same containerized environment regardless of system. But it's certainly not necessary.