1

I wrote a Hello World Spring Boot app. It's a Maven project.

localhost:8080

Now I want to dockerize this app. It is important that the .jar is created by Docker.

Dockerfile:

FROM maven:3.5-jdk-8 AS build
WORKDIR /app
COPY src .
COPY pom.xml .
RUN mvn -f pom.xml clean package

FROM openjdk:8-alpine
COPY --from=build /src/app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]

The creation keeps failing. What am I doing wrong in the Dockerfile? I got the basic idea of the Dockerfile from this post. How to dockerize maven project? and how many ways to accomplish it?

Alex
  • 125
  • 2
  • 12

2 Answers2

1

first build/clean your app with Maven, then a .jar file (or .war) will be in the directory /target in the Dockerfile add this:

FROM openjdk:12
ADD target/<jar-name-in-target>.jar docker-spring.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "docker-spring.jar"]

then build your Dockerfile

sdzt9
  • 160
  • 7
  • 1
    It is important that the .jar file is built by Docker so that I can use GitLab-CI / CD. – Alex Nov 11 '20 at 23:22
0

The following dockerfile works.

# Build stage
FROM maven:3.6.3-jdk-8-slim AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean test package

# Package stage
FROM openjdk:8-jdk-alpine
COPY --from=build /home/app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]
Alex
  • 125
  • 2
  • 12