0

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
victor
  • 1
  • 2
  • Replace last COPY with `RUN cp`: source `target/*.jar` is built by naven within the layer, so does not belong to host filesystem. – STerliakov Nov 23 '22 at 15:55
  • Did you tried with the same Dockerfile mentioned here https://stackoverflow.com/a/27768965/7871511? – Rohit Agarwal Nov 23 '22 at 18:09
  • please (best) stick to [quickstart](https://spring.io/guides/gs/spring-boot-docker/) and [ref. doc](https://docs.spring.io/spring-boot/docs/current/reference/html/container-images.html) – xerx593 Nov 23 '22 at 19:03

2 Answers2

1

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
Ronak Jain
  • 3,073
  • 1
  • 11
  • 17
0

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

victor
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 26 '22 at 12:40