11# syntax=docker/dockerfile:1
2-
3- # the runner is built
2+ # ==============================================================================
3+ # STAGE 1: BUILD STAGE
4+ # Build the application using Maven in an Alpine-based container
5+ # ==============================================================================
46FROM maven:3.9.5-eclipse-temurin-17-alpine AS builder
57
68ARG BUILD_HOME=/home/app
79ARG BUILD_PROFILE=postgres
810
9- # Name of the directory
11+ # instance configuration: JKU, MUG, TUG
1012ARG INSTANCE_NAME
1113
12- RUN mkdir $BUILD_HOME && mkdir -p $BUILD_HOME/.m2/repository && chown -R 1000:0 $BUILD_HOME
14+ # build directories with proper permissions for non-root user
15+ RUN mkdir $BUILD_HOME && \
16+ mkdir -p $BUILD_HOME/.m2/repository && \
17+ chown -R 1000:0 $BUILD_HOME
18+
19+ # Switch to non-root user for security
1320USER 1000
1421WORKDIR $BUILD_HOME
1522
23+ # copies from instances/${INSTANCE_NAME}/ directory
1624COPY instances/${INSTANCE_NAME}/src ./src
1725COPY instances/${INSTANCE_NAME}/pom.xml .
1826
27+ # Maven repository volume for caching dependencies
1928VOLUME ["/home/app/.m2/repository" ]
29+
30+ # build the application
2031RUN mvn -Duser.home=$BUILD_HOME -B package -DskipTests -Dquarkus.profile=${BUILD_PROFILE}
2132
22- # Create a second stage container which will only contain the runtime binaries without build dependencies
33+ # ==============================================================================
34+ # STAGE 2: RUNTIME STAGE
35+ # Create a lightweight container with only the required dependencies to run the app
36+ # ==============================================================================
2337FROM rockylinux:8.5 AS runner
2438
2539ARG JAVA_PACKAGE=java-17-openjdk-headless
2640ARG RUN_JAVA_VERSION=1.3.8
27-
28- # path to copy built binaries from builder container
2941ARG BUILD_HOME=/home/app
3042
3143ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en'
3244
33- # install java and the run-java script and set up permissions for the unprivileged 1001 container user
34- RUN dnf install -y openssl tzdata-java curl ca-certificates ${JAVA_PACKAGE} \
35- && dnf clean all -y \
36- && mkdir /deployments \
37- && chown 1001 /deployments \
38- && chmod "g+rwX" /deployments \
39- && chown 1001:root /deployments \
40- && curl https://repo1.maven.org/maven2/io/fabric8/run-java-sh/${RUN_JAVA_VERSION}/run-java-sh-${RUN_JAVA_VERSION}-sh.sh -o /deployments/run-java.sh \
41- && chown 1001 /deployments/run-java.sh \
42- && chmod 540 /deployments/run-java.sh \
43- && echo "securerandom.source=file:/dev/urandom" >> /etc/alternatives/jre/lib/security/java.security
44-
45- # configure the JAVA_OPTIONS, you can add -XshowSettings:vm to also display the heap size.
45+ # install runtime dependencies and set up deployment directory
46+ RUN dnf install -y openssl tzdata-java curl ca-certificates ${JAVA_PACKAGE} && \
47+ dnf clean all -y && \
48+ # Create deployment directory with proper permissions
49+ mkdir /deployments && \
50+ chown 1001 /deployments && \
51+ chmod "g+rwX" /deployments && \
52+ chown 1001:root /deployments && \
53+ # Download and install run-java script for optimized JVM startup
54+ curl https://repo1.maven.org/maven2/io/fabric8/run-java-sh/${RUN_JAVA_VERSION}/run-java-sh-${RUN_JAVA_VERSION}-sh.sh -o /deployments/run-java.sh && \
55+ chown 1001 /deployments/run-java.sh && \
56+ chmod 540 /deployments/run-java.sh && \
57+ # Optimize JVM random number generation for faster startup
58+ echo "securerandom.source=file:/dev/urandom" >> /etc/alternatives/jre/lib/security/java.security
59+
60+ # configure JVM options for Quarkus application
4661ENV JAVA_OPTIONS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager -Duser.home=/deployments"
4762
48- # copy runtime binaries to /deployments folder on runner container, the run-java script will pick this up
49- # and start the application
63+ # copy compiled application from builder stage
5064COPY --from=builder $BUILD_HOME/target/quarkus-app/lib/ /deployments/lib/
5165COPY --from=builder $BUILD_HOME/target/quarkus-app/*.jar /deployments/
5266COPY --from=builder $BUILD_HOME/target/quarkus-app/app/ /deployments/app/
5367COPY --from=builder $BUILD_HOME/target/quarkus-app/quarkus/ /deployments/quarkus/
5468
69+ # expose application port
5570EXPOSE 8080
5671
57- # for Openshift based unprivilegued Kubernetes environments, we will set the user to 1001
72+ # user 1001 is standard for OpenShift and Kubernetes environments
5873USER 1001
5974
6075ENTRYPOINT [ "/deployments/run-java.sh" ]
0 commit comments