3

so im making a spring boot application that im supposed to put in and run from a docker container, and i would like to build the whole image using a docker file.
im using this dockerFile:

FROM openjdk:8-jdk-alpine
ADD . /analytics-service
WORKDIR /analytics-service
ENTRYPOINT ./mvnw spring-boot:run 

when i create the image it just copies the files, and only after i run it, it starts downloading all the maven dependencies. which takes a while, given that i will be running a few containers. so how do i do it ? i want it to get all the dependencies when the image is created, so when i create a container it doesnt start downloading.

1 Answers1

5

If I understood you correctly, you would like to have the Maven dependencies downloaded first then combine them with your app into an image?

If that's what you want then the proper way to do that is as follows:

  1. Pull a maven image (normally you call this stage the 'builder' - it's just a name)
  2. Copy your pom.xml file into a working directory
  3. Run maven once to get your dependencies and then again to package it up
  4. Create a new image base from openjdk
  5. Copy the result of step 3 into your app image
  6. Expose a port
  7. Provide an entry point

Here's what that looks like in a Dockerfile:

FROM maven AS builder
WORKDIR /usr/src/analytics
COPY pom.xml .
RUN mvn -B dependency:go-offline

COPY . .
RUN mvn package

FROM openjdk:8-jdk-alpine
WORKDIR /analytics-service
COPY --from=builder /usr/src/analytics/target/YOUR_JAR_FILENAME.jar .
EXPOSE 80
ENTRYPOINT ["java", "-jar", "/analytics-service/YOUR_JAR_FILENAME.jar"]

You'll need to know how your jar file is named before you run this. You can run mvn package outside of Docker on your computer and see the filename that is generated. Copy that into the two spots above in the Dockerfile.

Andrew
  • 80
  • 7
  • 1
    Do i have to package it into a jar?, is there a way to just create the container, get the dependencies on it before the ENTRYPOINT commands are invoked ?, the way its working now is that when i create the image it only has the code, but when it starts it starts pulling the dependencies, i want it so that the image created by the docker file has the dependencies on it. i know your solution does that , but is there a way without the jar packaging ? – Grim Ranger May 07 '22 at 02:27
  • 1
    Yeah, the command ```RUN mvn -B dependency:go-offline``` does that for you. If you run that then maven will make sure you have everything (according to your pom.xml file) and won't download the dependencies when your app starts. – Andrew May 07 '22 at 02:49
  • 1
    ok great, and can i just add that command to the docker file in my post before the ENTRYPOINT command ? or do i need to get the maven image ? how exactly would the dockerfile be ? thx – Grim Ranger May 07 '22 at 02:57
  • 1
    I believe there is a way to do what you want without a .jar but it may be a bit cumbersome. My suggestion is to have everything packaged into a .jar and then run that. One thing to keep in mind is that you are using mvw in your original Dockerfile. mvw is a wrapper for maven which means that you are downloading it every single time you run your container. This may be what is also causing you problems. Ideally you would create a base image from maven (like in my Dockerfile above), install your dependencies, package it into a jar file and then turn that into its own image. – Andrew May 07 '22 at 03:29
  • ok i'll look more into it , thanks for the help – Grim Ranger May 07 '22 at 03:38
  • hey, not sure if your still here , but it seems that when i package my app into a jar, and copy it to the container , it doesnt seem to see the JSPs , any ideas ? – Grim Ranger May 08 '22 at 01:01
  • I’m not that well versed in JSPs but see if this helps: https://stackoverflow.com/questions/21243690/is-it-possible-with-spring-boot-to-serve-up-jsps-with-a-jar-packaging – Andrew May 08 '22 at 06:10
  • if there are other jars that are not packed into single, they must be located somewhere in context of the docker file to be able to copy them into the container, right? – mike01010 Apr 02 '23 at 01:02