Home > Enterprise >  How to generate a jar file in the Dockerfile?
How to generate a jar file in the Dockerfile?

Time:11-24

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

CodePudding user response:

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

CodePudding user response:

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

  • Related