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?
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 changeIdk what the equivalent command is in gradle but I’m sure there is one
1
u/wishicouldcode Intermediate Brewer May 04 '19
Never seen this before. I thought maven/gradle are not available in most base images. Is the 'as builder' instruction in Dockerfile making it available in the alpinr image?
3
u/shagieIsMe Extreme Brewer May 04 '19 edited May 04 '19
That's a multi stage docker build.
Maven and gradle aren't part of the jdk image.
~ $ docker run -it --tty --rm openjdk:8-jdk-alpine sh / # which maven / # which gradle / # which javac /usr/lib/jvm/java-1.8-openjdk/bin/javacThe key part to the multi stage docker build (docs) is the copy line:
COPY --from=builder /src/build/libs/*-0.0.1-SNAPSHOT.jar .Giving it a name makes it easier, but it isn't necessary.
FROM golang:1.7.3 WORKDIR /go/src/github.com/alexellis/href-counter/ RUN go get -d -v golang.org/x/net/html COPY app.go . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . FROM alpine:latest RUN apk --no-cache add ca-certificates WORKDIR /root/ COPY --from=0 /go/src/github.com/alexellis/href-counter/app . CMD ["./app"]How does it work? The second
FROMinstruction starts a new build stage with thealpine:latestimage as its base. TheCOPY --from=0line copies just the built artifact from the previous stage into this new stage. The Go SDK and any intermediate artifacts are left behind, and not saved in the final image.edit:
Doing a build of a multi-module that I have based off of gradle... the build image and the artifact image:
REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> c85400308be5 3 seconds ago 115MB <none> <none> faad64d9a8f9 6 seconds ago 754MBThere's a bit of a size difference on what I'd want to deploy. It's easier to make an artifact image than trying to clean up the build image (and then you get into messes about the size of the layers when cleaning up).
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?
1
u/probable-maybe May 04 '19
I now realise I failed to describe the issue clearly and focused on the aspect I misunderstood anyway (building the jar is consistent across machines). The actual use case was to be able to run
docker-compose up -dand deploy everything regardless of whether Java exists on the machine which is why having it built inside a container is useful imo
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
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.
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/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.
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:
Multi stage Docker build
Correct ordering of COPY and RUN build commands to use Docker layers as a dependency cache
Distroless base image for final image
GoLang health check (no shell available in the final image)
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.
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.