admin管理员组

文章数量:1122832

I want to reduce my Docker image size which should contain the JDK 17 and my app.jar. I decided to do it using jlink by extracting the JDK which would include only the needed modules my application needs to run.

The main process of extracting the JDK looks like this:

RUN jar xf ./target/app.jar
RUN jdeps --ignore-missing-deps --print-module-deps --multi-release 17 --recursive --class-path ./BOOT-INF/lib/* ./target/app.jar > modules.txt
RUN jlink --add-modules $(cat modules.txt) --strip-debug --no-man-pages --no-header-files --output jre-17

It works perfectly on my local machine (Windows 10) but doesn't in Docker (openjdk:17-alpine). I get this error:

------
 > [auth build 7/8] RUN jdeps --ignore-missing-deps --print-module-deps --multi-release 17 --recursive --class-path ./BOOT-INF/lib/* ./target/app.jar > modules.txt:
5.271 Exception in thread "main" java.lang.module.FindException: Module jakarta.cdi not found, required by jakarta.transaction
5.347   at java.base/java.lang.module.Resolver.findFail(Resolver.java:893)
5.347   at java.base/java.lang.module.Resolver.resolve(Resolver.java:192)
5.347   at java.base/java.lang.module.Resolver.resolve(Resolver.java:141)
5.347   at java.base/java.lang.module.Configuration.resolve(Configuration.java:421)
5.350   at java.base/java.lang.module.Configuration.resolve(Configuration.java:255)
5.355   at jdk.jdeps/com.sun.tools.jdeps.JdepsConfiguration$Builder.build(JdepsConfiguration.java:564)
5.355   at jdk.jdeps/com.sun.tools.jdeps.JdepsTask.buildConfig(JdepsTask.java:603)
5.355   at jdk.jdeps/com.sun.tools.jdeps.JdepsTask.run(JdepsTask.java:557)
5.355   at jdk.jdeps/com.sun.tools.jdeps.JdepsTask.run(JdepsTask.java:533)
5.355   at jdk.jdeps/com.sun.tools.jdeps.Main.main(Main.java:49)
------
failed to solve: process "/bin/sh -c jdeps --ignore-missing-deps --print-module-deps --multi-release 17 --recursive --class-path ./BOOT-INF/lib/* ./target/app.jar > modules.txt" did not complete successfully: exit code: 1

Here's my Dockerfile:

# Build application
FROM openjdk:17-alpine AS build

# Install Maven
RUN apk update && apk add wget && apk add binutils
RUN wget .9.9/binaries/apache-maven-3.9.9-bin.tar.gz \
    && tar -xzvf apache-maven-3.9.9-bin.tar.gz -C /opt/ \
    && rm apache-maven-3.9.9-bin.tar.gz
ENV PATH=$PATH:/opt/apache-maven-3.9.9/bin

WORKDIR /build

COPY . .

# RUN mvn clean package -DskipTests

RUN jar xf ./target/app.jar
RUN jdeps --ignore-missing-deps --print-module-deps --multi-release 17 --recursive --class-path ./BOOT-INF/lib/* ./target/app.jar > modules.txt
RUN RUN jlink --add-modules $(cat modules.txt) --strip-debug --no-man-pages --no-header-files --output jre-17


# Run application
FROM alpine:latest

WORKDIR /jre

COPY --from=build /build/jre-17 .

ENV JAVA_HOME /jre

ENV PATH=$PATH:$JAVA_HOME/bin

WORKDIR /app

COPY --from=build /build/target/app.jar .

CMD ["java", "-jar", "./app.jar"]

What is wrong with my Dockerfile?

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=".0.0" xmlns:xsi=";
    xsi:schemaLocation=".0.0 .0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>arkadisahakyan</groupId>
    <artifactId>authentication-with-spring</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>authentication-with-spring</name>
    <description>Demo project</description>
    <properties>
        <java.version>17</java.version>
        <start-class>arkadisahakyan.authenticationwithspring.AuthenticationWithSpringApplication</start-class>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity6</artifactId>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>2.2.224</version>
        </dependency>
        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>5.12.0</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>app</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

本文标签: