Skip to content

Commit

Permalink
hotfix: reduce size docker image file.
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardoMeireles55 committed Jan 26, 2025
1 parent 768d618 commit e1bce06
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
63 changes: 53 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,63 @@
FROM eclipse-temurin:21-jdk-alpine AS build
# First stage, build the custom JRE
FROM eclipse-temurin:21-jdk-alpine AS jre-builder

RUN apk add --no-cache maven
RUN mkdir /opt/app
COPY src /opt/app/src
COPY pom.xml /opt/app

COPY src /app/src
COPY pom.xml /app
WORKDIR /opt/app

ENV MAVEN_VERSION 3.5.4
ENV MAVEN_HOME /usr/lib/mvn
ENV PATH $MAVEN_HOME/bin:$PATH

RUN apk update && \
apk add --no-cache tar binutils maven

WORKDIR /app
RUN mvn clean package -DskipTests -U \
&& rm -rf /root/.m2 \
&& rm -rf /app/src
&& rm -rf /opt/app/src

RUN jar xvf target/QualityLabPro-0.7.jar
RUN jdeps --ignore-missing-deps -q \
--recursive \
--multi-release 21 \
--print-module-deps \
--class-path 'BOOT-INF/lib/*' \
target/QualityLabPro-0.7.jar > modules.txt

# Build small JRE image
RUN $JAVA_HOME/bin/jlink \
--verbose \
--add-modules $(cat modules.txt) \
--strip-debug \
--no-man-pages \
--no-header-files \
--compress=2 \
--output /optimized-jdk-21

# Second stage, Use the custom JRE and build the app image
FROM alpine:latest
ENV JAVA_HOME=/opt/jdk/jdk-21
ENV PATH="${JAVA_HOME}/bin:${PATH}"

# Run stage
FROM eclipse-temurin:21-jre-alpine
# copy JRE from the base image
COPY --from=jre-builder /optimized-jdk-21 $JAVA_HOME

# Add app user
ARG APPLICATION_USER=spring

# Create a user to run the application, don't run as root
RUN addgroup --system $APPLICATION_USER && adduser --system $APPLICATION_USER --ingroup $APPLICATION_USER

# Create the application directory
RUN mkdir /app && chown -R $APPLICATION_USER /app

COPY --chown=$APPLICATION_USER:$APPLICATION_USER --from=jre-builder /opt/app/target/QualityLabPro-0.7.jar /app/app.jar

WORKDIR /app

WORKDIR /usr/src/app
COPY --from=build /app/target/QualityLabPro-0.7.jar ./app.jar
USER $APPLICATION_USER

ENV SPRING_PROFILES_ACTIVE=prod \
SERVER_PORT=8080
Expand Down
22 changes: 22 additions & 0 deletions Dockerfile_OLD
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM eclipse-temurin:21-jdk-alpine AS build

RUN apk add --no-cache maven

COPY src /app/src
COPY pom.xml /app

WORKDIR /app
RUN mvn clean package -DskipTests -U \
&& rm -rf /root/.m2 \
&& rm -rf /app/src

# Run stage
FROM eclipse-temurin:21-jre-alpine

WORKDIR /usr/src/app
COPY --from=build /app/target/QualityLabPro-0.7.jar ./app.jar

ENV SPRING_PROFILES_ACTIVE=prod \
SERVER_PORT=8080

EXPOSE ${SERVER_PORT}

0 comments on commit e1bce06

Please sign in to comment.