1- # Start with a base image containing Java runtime
2- FROM openjdk:8-jdk-alpine
1+ # ### Stage 1: Build the application
2+ FROM openjdk:8-jdk-alpine as build
33
4- # Add Maintainer Info
5- LABEL maintainer= "callicoder@gmail.com"
4+ # Set the current working directory inside the image
5+ WORKDIR /app
66
7- # Add a volume pointing to /tmp
8- VOLUME /tmp
7+ # Copy maven executable to the image
8+ COPY mvnw .
9+ COPY .mvn .mvn
910
10- # Make port 8080 available to the world outside this container
11- EXPOSE 8080
11+ # Copy the pom.xml file
12+ COPY pom.xml .
1213
13- # The application's jar file
14- ARG JAR_FILE=target/polls-0.0.1-SNAPSHOT.jar
14+ # Build all the dependencies in preparation to go offline.
15+ # This is a separate step so the dependencies will be cached unless
16+ # the pom.xml file has changed.
17+ RUN ./mvnw dependency:go-offline -B
1518
16- # Add the application's jar to the container
17- ADD ${JAR_FILE} app.jar
19+ # Copy the project source
20+ COPY src src
1821
19- # Run the jar file
20- ENTRYPOINT ["java" ,"-Djava.security.egd=file:/dev/./urandom" ,"-jar" ,"/app.jar" ]
22+ # Package the application
23+ RUN ./mvnw package -DskipTests
24+ RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)
25+
26+ # ### Stage 2: A minimal docker image with command to run the app
27+ FROM openjdk:8-jre-alpine
28+
29+ ARG DEPENDENCY=/app/target/dependency
30+
31+ # Copy project dependencies from the build stage
32+ COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
33+ COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF
34+ COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app
35+
36+ ENTRYPOINT ["java" ,"-cp" ,"app:app/lib/*" ,"com.example.polls.PollsApplication" ]
0 commit comments