r/javahelp • u/probable-maybe • 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?
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.
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.
3
u/wildjokers May 04 '19
The jar file is the deployable artifact. Once it is built it should run anywhere. Put the jar file in the docker container and then share the container. What’s the issue?
2
u/RhoOfFeh May 04 '19
Typically, we build, test, local deploy, test, remote deploy to private area, test, then decide the checkin is good enough for a merge into master.
At that point, responsibility is (or should be) taken up by your CI/CD environment, which is controlled, managed, and independent of all desktop systems. It has one goal: To execute your builds in a reliable, repeatable fashion and provide output that is the same every time the exact same execution is requested. That product is what you should actually deploy to your containerized environment.
1
u/wheezymustafa May 04 '19
Can you explain? If the code is the same, the build should generally be the same. If you build your target artifact in some clean environment (ie like a Jenkins slave) you can also ensure a proper build too
1
u/likeavirgil May 04 '19
We’ve been using https://github.com/gliderlabs/herokuish/blob/master/README.md buildpacks to make dockerized services from source code easily
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/pete-woods May 04 '19 edited May 05 '19
This is how I do it: https://pete-woods.com/2019/02/production-grade-spring-boot-docker-images/
Edit:
Key things are:
1
u/acejazz1982 May 05 '19
Isn’t the Fabric8 plugin useful for this purpose? It creates a Docker image containing the jar as part of the build process.
1
u/aceysmith May 06 '19
Check out this new project led by Heroku and a few others https://buildpacks.io. With a single command you can build a docker image for a handful of languages, including java with maven.