Last active
April 10, 2022 15:46
-
-
Save wesleyegberto/1cf549b2661a199208d9e2d3d37c5344 to your computer and use it in GitHub Desktop.
Multistage Dockerfile to build and run a Java project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM maven:3.6-jdk-11 as builder | |
WORKDIR /app | |
COPY pom.xml . | |
# download dependencies as specified in pom.xml | |
# building dependency layer early will speed up compile time when pom is unchanged | |
RUN mvn verify --fail-never | |
COPY src ./src | |
# Build a release artifact. | |
RUN mvn package | |
# Use AdoptOpenJDK for base image. | |
# It's important to use OpenJDK 8u191 or above that has container support enabled. | |
FROM adoptopenjdk/openjdk11-openj9:alpine-slim | |
RUN addgroup -S spring && adduser -S spring -G spring | |
USER spring:spring | |
# Copy the jar to the production image from the builder stage. | |
COPY --from=builder /app/target/my-app*.jar /my-app.jar | |
# Faster if less then 4 CPUs: -Dspring.backgroundpreinitializer.ignore=true | |
CMD ["java", "-noverify", "-Djava.security.egd=file:/dev/./urandom", "-jar", "my-app.jar", "--spring.jmx.enabled=false"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment