Setting the file. One moment.
Java · Azure Kubernetes App Deploy · microsoft/azure-skills · Skills Docs
ContentsBack to the top of the page (opens in a new tab)
templates/dockerfiles/ java.Dockerfile
DOCKERFILE · 78 lines · 3 KB
15 # RUN ./gradlew bootJar --no-daemon
16 # And adjust the JAR_FILE path to "build/libs/*.jar"
17 # =============================================================================
18
19 # ---------------------------------------------------------------------------
20 # Stage 1: Build
21 # ---------------------------------------------------------------------------
22 # Base: current Java LTS (Temurin). See references/base-images.md for the Microsoft OpenJDK alternative.
23 FROM eclipse-temurin:<LATEST_STABLE_JAVA_JDK>-jdk-alpine AS build
24
25 WORKDIR /app
26
27 # Layer caching: copy Maven wrapper and POM first so dependency resolution is
28 # cached independently of source changes.
29 COPY mvnw pom.xml ./
30 COPY .mvn .mvn
31
32 # Download dependencies (offline-friendly layer)
33 RUN chmod +x mvnw \
34 && ./mvnw dependency:go-offline -B
35
36 # Copy source and build the fat JAR
37 COPY src ./src
38
39 # -Dspring-boot.repackage.finalName=app ensures a single predictably named fat JAR,
40 # avoiding glob ambiguity when Maven produces both thin and fat JARs.
41 RUN ./mvnw package spring-boot:repackage -DskipTests -B \
42 -Dspring-boot.repackage.finalName=app \
43 && mv target/app.jar app.jar
44
45 # ---------------------------------------------------------------------------
46 # Stage 2: Runtime
47 # ---------------------------------------------------------------------------
48 FROM eclipse-temurin:<LATEST_STABLE_JAVA_JRE>-jre-alpine
49
50 WORKDIR /app
51
52 # AKS Deployment Safeguards DS004: create and switch to a non-root user
53 RUN addgroup -S appuser && adduser -S appuser -G appuser
54
55 # Copy only the built JAR from the build stage
56 COPY --from=build --chown=appuser:appuser /app/app.jar ./app.jar
57
58 # Spring Boot Layered JARs: if using layered JARs, replace the COPY above
59 # with the extract + copy approach for even better layer caching:
60 # RUN java -Djarmode=layertools -jar app.jar extract
61 # COPY --from=build /app/dependencies/ ./
62 # COPY --from=build /app/spring-boot-loader/ ./
63 # COPY --from=build /app/snapshot-dependencies/ ./
64 # COPY --from=build /app/application/ ./
65
66 USER appuser
67
68 EXPOSE 8080
69
70 # MaxRAMPercentage caps heap relative to the container memory limit
71 # (container-aware by default in JDK 21).
72 ENV JAVA_OPTS= "-XX:MaxRAMPercentage=75.0 -XX:+UseG1GC"
73
74 # No HEALTHCHECK: the JRE Alpine image does not include wget or curl.
75 # Kubernetes liveness/readiness probes (configured in deployment.yaml) handle
76 # health checking in AKS.
77
78 ENTRYPOINT [ "sh" , "-c" , "exec java $JAVA_OPTS -jar app.jar" ]