skip to Main Content

I’m trying to dockerize a spring boot app but i’m having trouble building a jar file with maven.
I Already tried to follow this tutorial but somehow my .jar isn’t being updated by the ‘mvn package’ command inside the Dockerfile.

If I manually run ‘mvn package’ and then build the image, it works.

this is my dockerfile

FROM openjdk:11
FROM maven:3.8-jdk-11 as maven_build
COPY pom.xml pom.xml
COPY src src
RUN mvn clean package
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

my project structure

Demo
└── src
|    ├── main
|    │   ├── java
|    │       └── com
|    │           └── App.java
|    │               
|    │   
|    └── test
|
├──── Dockerfile
├──── pom.xml

2

Answers


  1. Chosen as BEST ANSWER

    turns out that you can just skip the Dockerfile Altogether in this case and use this maven command instead:

    ./mvnw spring-boot:build-image
    

    and it will build a docker image for you


  2. You need to use –from to copy an artifact built in the previous stage to the current stage.

    Just replace

    COPY ${JAR_FILE} app.jar
    

    with

    COPY --from=maven_build /path/to/target/*.jar /app.jar
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search