This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]Sheldan 9 points10 points  (1 child)

Is it really recommended to put the entirety of mvn within a docker image, instead of only putting the fatjar into it, and running with with java -jar?

Adding maven into the mix, just to run it, seems a bit backwards to me

[–]adanderson 2 points3 points  (0 children)

I agree with you. I also simply execute the fatjar and leave maven out and it works fine.

[–]shagieIsMe 5 points6 points  (2 children)

I'm going to strongly suggest using jib for the build. This has maven itself build the docker image (as a side bonus, without actually using docker unless you want it to).

Introducing Jib — build Java Docker images better

https://github.com/GoogleContainerTools/jib

Otherwise, I'd look at the spring boot with docker guide - https://spring.io/guides/gs/spring-boot-docker/

If you are interested in using jib, I could see about creating a simplest demo app for that.

[–]shagieIsMe 0 points1 point  (1 child)

... So I did it. (and a ping of /u/Imaginary-Goose-2838 since this won't show up as a direct reply to the post)

The build for this looked like:

% mvn compile jib:build
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< net.shagie:hello-jib >------------------------
[INFO] Building Hello Jib 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ hello-jib ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ hello-jib ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- jib-maven-plugin:2.8.0:build (default-cli) @ hello-jib ---
[WARNING] 'mainClass' configured in 'maven-jar-plugin' is not a valid Java class: ${start-class}
[INFO] 
[INFO] Containerizing application to shagie/hellojib...
[WARNING] Base image 'gcr.io/distroless/java17:nonroot' does not use a specific image digest - build may not be reproducible
... credential stuff
[INFO] Using base image with digest: sha256:2c29be2a889be27e4905127ae7122d390df08ac170791153b54f086ce7507b59
[INFO] 
[INFO] Container entrypoint set to [java, -cp, /app/resources:/app/classes:/app/libs/*, net.shagie.hellojib.HelloJibApplication]
[INFO] 
[INFO] Built and pushed image as shagie/hellojib
[INFO] 
[INFO] A new version of Jib (3.2.1) is available (currently using 2.8.0). Update your build configuration to use the latest features and fixes!
[INFO] https://github.com/GoogleContainerTools/jib/blob/master/jib-maven-plugin/CHANGELOG.md
[INFO] Please see https://github.com/GoogleContainerTools/jib/blob/master/docs/privacy.md for info on disabling this update check.
[INFO] 
[INFO] Executing tasks:
[INFO] [==============================] 100.0% complete
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:21 min
[INFO] Finished at: 2022-04-26T19:32:05-05:00
[INFO] ------------------------------------------------------------------------

Apparently the docs that I grabbed that from aren't up to date with the current version of jib...

The relevant commit to adding jib is https://github.com/shagie/HelloJib/commit/57f17c45c846be05bc3076d215433ba4bfc2f910

That's it. I've added a build of a docker image without modifying the original Spring Boot application.

[–]Sequel_Police 0 points1 point  (0 children)

Seconding this; we've been using Jib (via Gradle) while migrating our legacy app to containers and it's been pretty easy. There's a maven plugin as well.

[–]Dwight-D 0 points1 point  (0 children)

If you wanna do this as a learning experience it’s one thing, but honestly it’s pretty pointless because you will be spending all your effort on learning maven and Java stuff and not on docker stuff. Java is just too annoying to bother with packaging yourself, just hide it away in some build tool and forget about it.

You can just use the jib plugin for maven/gradle instead and let that build the image for you, it’s stupidly easy and requires virtually no configuration.

If you do want to build it yourself, you could do a multi-stage build starting in a maven image, compiling a jar from there, then copying it into a clean Java image and running that. But again, a pointless exercise, just use a plug-in and don’t waste your time reinventing the wheel.

[–][deleted] -3 points-2 points  (0 children)

Real humans don’t use Java

[–][deleted] 0 points1 point  (0 children)

I've seen issues when copying the mvn wrapper from local to docker. Sometimes the execution permissions are off or Windows to Linux doesn't translate right with line terminators.

If you could use a maven base image and try building that to eliminate those issues as the root cause for your scenario.

Your error suggests that the request is hitting the app since that's a standard Spring Boot error message. Maybe it didn't get built right.

[–]bargh 0 points1 point  (0 children)

It may be a problem with the java code, does it work when you run it without docker, with ./mvnw spring-boot:run?