Skip to content

Instantly share code, notes, and snippets.

@wesleyegberto
Last active April 10, 2022 15:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesleyegberto/1cf549b2661a199208d9e2d3d37c5344 to your computer and use it in GitHub Desktop.
Save wesleyegberto/1cf549b2661a199208d9e2d3d37c5344 to your computer and use it in GitHub Desktop.
Multistage Dockerfile to build and run a Java project
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