Created
September 9, 2022 17:46
-
-
Save rafaribe/3ddc70c194d84daf3c2fd2ba353afa12 to your computer and use it in GitHub Desktop.
Java Spring Ultimate Dockerfile
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
##### PRE BUILD ARGUMENTS ###### | |
ARG UID=1001 | |
ARG GID=1001 | |
ARG USER=nonroot | |
ARG MVN_VERSION=3.8.6 | |
ARG JDK_VERSION=18 | |
##### Base Image with dependencies downloaded ###### | |
FROM --platform=$BUILDPLATFORM maven:${MVN_VERSION}-eclipse-temurin-${JDK_VERSION} as base | |
WORKDIR /app | |
ARG BUILDOS | |
ARG BUILDARCH | |
COPY pom.xml . | |
RUN --mount=type=cache,target=/root/.m2,rw mvn dependency:go-offline | |
##### Builder Image for the source code ###### | |
FROM base as builder | |
COPY src src | |
RUN mvn -e -DskipTests package | |
RUN java -Djarmode=layertools -jar /app/target/*.jar extract --destination target/extracted | |
################# RUNTIME #################### | |
FROM --platform=$BUILDPLATFORM eclipse-temurin:${JDK_VERSION}-jre-jammy as final | |
ARG UID | |
ARG GID | |
ARG USER | |
WORKDIR /app | |
################# SECURITY #################### | |
RUN groupadd ${USER} -r -g ${GID} && useradd ${USER} -g ${GID} -u ${UID} --no-create-home | |
ARG EXTRACTED=/app/target/extracted | |
COPY --from=builder --chown=${UID}:${GID} ${EXTRACTED}/dependencies/ ./ | |
COPY --from=builder --chown=${UID}:${GID} ${EXTRACTED}/spring-boot-loader/ ./ | |
COPY --from=builder --chown=${UID}:${GID} ${EXTRACTED}/snapshot-dependencies/ ./ | |
COPY --from=builder --chown=${UID}:${GID} ${EXTRACTED}/application/ ./ | |
################# LABELS #################### | |
ARG BUILD_DATE | |
ARG COMMIT_USER | |
LABEL org.opencontainers.image.created=$BUILD_DATE | |
LABEL org.opencontainers.image.authors=$COMMIT_USER | |
################# ENTRYPOINT #################### | |
USER ${USER} | |
ENTRYPOINT ["java", "-Xms512m", "-Xmx512m", "org.springframework.boot.loader.JarLauncher"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment