-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockerfile-mvn
49 lines (38 loc) · 1.85 KB
/
Dockerfile-mvn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# This Dockerfile builds and runs a Spring Boot application from a multi-module maven project
# It takes two build arguments:
# - MODULE is the path to the Spring Boot module, relative to the base pom file (e.g. module1/submodule2)
# - APP is the name of the generated Spring Boot jar file, normally the artifact id of the maven module
# First stage: Build the source code with maven
FROM maven:3-openjdk-11-slim AS build
WORKDIR /app
# Copy all POM files
# All modules need to be included, even if they are not required for the build
# The list can be generated by running this from the directory with the base pom file:
# find -name pom.xml -printf "COPY %p %h/\n" | sort -t" " -k3 | column -t
### [ Add COPY commands for pom files here ]
# Resolve all dependencies
RUN --mount=type=secret,id=m2-settings,dst=/root/.m2/settings.xml --mount=type=cache,target=/root/.m2/repository \
mvn -q -B dependency:go-offline
# Specify the required module
ARG MODULE
# Copy full source code (generated code is ignored through .dockerignore)
COPY ./ ./
# Build the requested module and dependencies
RUN --mount=type=cache,target=/root/.m2/repository \
mvn -q package -pl "${MODULE}" -am
# Extract the JAR
ARG APP
RUN java -Djarmode=layertools -jar /app/${MODULE}/target/${APP}-*.jar extract
# Second stage: Build the server image (needs only JRE)
FROM adoptopenjdk/openjdk11:alpine-jre AS runtime
# Run as non-root
RUN adduser --gecos "" --shell "/sbin/nologin" --disabled-password --no-create-home spring
USER spring
# Copy the classes and dependencies
WORKDIR /app
COPY --from=build /app/dependencies/ ./
COPY --from=build /app/spring-boot-loader/ ./
COPY --from=build /app/snapshot-dependencies/ ./
COPY --from=build /app/application/ ./
# Run the Spring Boot application
ENTRYPOINT ["java", "-Dspring.config.location=file:/config/", "org.springframework.boot.loader.JarLauncher"]