简体   繁体   English

docker 在 gitlab-runner 中构建失败但在本地工作:spring 使用 GraalVm 启动本机编译

[英]docker build fails inside gitlab-runner but works locally : spring boot native compilation with GraalVm

I made a Dockerfile to build my spring boot project with GraalVm natively;我制作了一个 Dockerfile 来使用 GraalVm 本机构建我的 spring 引导项目; everything went correctly.一切顺利。

Here is the Dockerfile这是Dockerfile

FROM ghcr.io/graalvm/graalvm-ce:22.3.1 AS buildnative

WORKDIR /app

COPY mvnw pom.xml ./
COPY .mvn/ .mvn
COPY src ./src
RUN ./mvnw clean package -Pnative

FROM ubuntu:23.04
EXPOSE 8080
COPY --from=buildnative /app/target/spring-boot-project /build/app
CMD ["/build/app"]

This runs perfectly locally, but in the GitLab runner, I always have the same error.这在本地运行完美,但在 GitLab 跑步者中,我总是有同样的错误。

JAVA_HOME is not defined correctly.
We cannot execute /opt/graalvm-ce-java17-22.3.1/bin/java 
The command '/bin/sh -c ./mvnw clean package -Pnative' returned a non-zero code: 1

So I decided to add some logs within the maven wrapper, and here is what I have:所以我决定在 maven 包装器中添加一些日志,这就是我所拥有的:

Step 7/11 : RUN ./mvnw clean package -Pnative ---> Running in 81e0558130f3 ------------> /opt/graalvm-ce-java17-22.3.1/bin/java ------------> JAVA_HOME is /opt/graalvm-ce-java17-22.3.1 Error: JAVA_HOME is not defined correctly. We cannot execute /opt/graalvm-ce-java17-22.3.1/bin/java The command '/bin/sh -c ./mvnw clean package -Pnative' returned a non-zero code: 1 Cleaning up project directory and file based variables

Step 7/11 : RUN ./mvnw clean package -Pnative
 ---> Running in 81e0558130f3
------------> /opt/graalvm-ce-java17-22.3.1/bin/java
------------> JAVA_HOME is /opt/graalvm-ce-java17-22.3.1
Error: JAVA_HOME is not defined correctly.
  We cannot execute /opt/graalvm-ce-java17-22.3.1/bin/java
The command '/bin/sh -c ./mvnw clean package -Pnative' returned a non-zero code: 1
Cleaning up project directory and file based variables

In the log I have added, we can see JAVA_HOME is defined and is adequately defined.在我添加的日志中,我们可以看到JAVA_HOME已定义并且已充分定义。 It is the same as locally, where everything works perfectly.它与本地一样,一切都完美无缺。

I tried to add this line: RUN chmod +x mvnw before running it, but it did not change anything.我试图添加这一行:运行它之前运行RUN chmod +x mvnw ,但它没有改变任何东西。

I need more ideas.我需要更多的想法。 Is there anyone have an idea of what is happening?有没有人知道发生了什么?

Edit:编辑:

I decided to dive deeper into the issue.我决定深入研究这个问题。 I have added logs to know why it does not work.我添加了日志以了解它为什么不起作用。 I modified the mvnw script to know what was happening.我修改了 mvnw 脚本以了解发生了什么。

I have added this to mvnw我已将此添加到mvnw

if [ -e "$JAVACMD" ] ; then
  echo "------------> THE FILE EXIST" >&2
else
  echo "------------> THE FILE DOES NOT EXIST" >&2
fi

if [ -x "$JAVACMD" ] ; then
  echo "------------> THE FILE IS EXECUTABLE" >&2
else
  echo "------------> THE FILE IS NOT EXECUTABLE" >&2
fi

Results:结果:

Here is in local:这是在本地:

------------> JAVACMD /opt/graalvm-ce-java17-22.3.1/bin/java 
------------> THE FILE EXIST 
------------> THE FILE IS EXECUTABLE 

Here is in the gitlab-runner:这是在 gitlab-runner 中:

------------> JAVACMD /opt/graalvm-ce-java17-22.3.1/bin/java
------------> THE FILE EXIST 
------------> THE FILE IS NOT EXECUTABLE 

Makes no sense to me对我来说毫无意义

Correct compile command is正确的编译命令是

mvn -Pnative native:compile

You can see more details and full doc here .您可以在此处查看更多详细信息和完整文档。 After that you will see graalvm build result in the logs.之后您将在日志中看到 graalvm 构建结果。 So you need to change your docker file like below所以你需要改变你的 docker 文件如下

FROM ghcr.io/graalvm/graalvm-ce:22.3.1 AS buildnative

WORKDIR /app

COPY mvnw pom.xml ./
COPY .mvn/ .mvn
COPY src ./src
RUN ./mvnw native:compile -Pnative

FROM ubuntu:23.04
EXPOSE 8080
COPY --from=buildnative /app/target/spring-boot-project /build/app
CMD ["/build/app"]

example build log from my local build来自我本地构建的示例构建日志

[INFO] --- native-maven-plugin:0.9.19:compile (default-cli) @ demo ---
Downloading: Component catalog from www.graalvm.org
Processing Component: Native Image
Downloading: Component native-image: Native Image from github.com
Installing new component: Native Image (org.graalvm.native-image, version 22.3.1)
[INFO] Found GraalVM installation from JAVA_HOME variable.
[INFO] [graalvm reachability metadata repository for ch.qos.logback:logback-classic:1.4.5]: Configuration directory not found. Trying latest version.
[INFO] [graalvm reachability metadata repository for ch.qos.logback:logback-classic:1.4.5]: Configuration directory is ch.qos.logback/logback-classic/1.4.1
[INFO] Executing: /opt/graalvm-ce-java17-22.3.1/bin/native-image -cp /app/target/classes:/root/.m2/repository/org/springframework/spring-aop/6.0.4/spring-aop-6.0.4.jar:/root/.m2/repository/org/springframework/boot/spring-boot-starter-logging/3.0.2/spring-boot-starter-logging-3.0.2.jar:/root/.m2/repository/org/springframework/spring-context/6.0.4/spring-context-6.0.4.jar:/root/.m2/repository/org/springframework/spring-core/6.0.4/spring-core-6.0.4.jar:/root/.m2/repository/org/apache/logging/log4j/log4j-api/2.19.0/log4j-api-2.19.0.jar:/root/.m2/repository/org/springframework/spring-expression/6.0.4/spring-expression-6.0.4.jar:/root/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.19.0/log4j-to-slf4j-2.19.0.jar:/root/.m2/repository/ch/qos/logback/logback-core/1.4.5/logback-core-1.4.5.jar:/root/.m2/repository/jakarta/annotation/jakarta.annotation-api/2.1.1/jakarta.annotation-api-2.1.1.jar:/root/.m2/repository/org/springframework/spring-beans/6.0.4/spring-beans-6.0.4.jar:/root/.m2/repository/ch/qos/logback/logback-classic/1.4.5/logback-classic-1.4.5.jar:/root/.m2/repository/org/springframework/boot/spring-boot-starter/3.0.2/spring-boot-starter-3.0.2.jar:/root/.m2/repository/org/springframework/spring-jcl/6.0.4/spring-jcl-6.0.4.jar:/root/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/3.0.2/spring-boot-autoconfigure-3.0.2.jar:/root/.m2/repository/org/slf4j/jul-to-slf4j/2.0.6/jul-to-slf4j-2.0.6.jar:/root/.m2/repository/org/yaml/snakeyaml/1.33/snakeyaml-1.33.jar:/root/.m2/repository/org/springframework/boot/spring-boot/3.0.2/spring-boot-3.0.2.jar:/root/.m2/repository/org/slf4j/slf4j-api/2.0.6/slf4j-api-2.0.6.jar --no-fallback -H:Path=/app/target -H:Name=demo -H:ConfigurationFileDirectories=/app/target/graalvm-reachability-metadata/160481799c4b6c37cde925c9aebf513c32245dcf/ch.qos.logback/logback-classic/1.4.1
========================================================================================================================
GraalVM Native Image: Generating 'demo' (executable)...
========================================================================================================================
[1/7] Initializing...                                                                                    (6.6s @ 0.23GB)
 Version info: 'GraalVM 22.3.1 Java 17 CE'
 Java version info: '17.0.6+10-jvmci-22.3-b13'
 C compiler: gcc (redhat, x86_64, 11.3.1)
 Garbage collector: Serial GC
 1 user-specific feature(s)
 - org.springframework.aot.nativex.feature.PreComputeFieldFeature
Field org.apache.commons.logging.LogAdapter#log4jSpiPresent set to true at build time
Field org.apache.commons.logging.LogAdapter#log4jSlf4jProviderPresent set to true at build time
Field org.apache.commons.logging.LogAdapter#slf4jSpiPresent set to true at build time
Field org.apache.commons.logging.LogAdapter#slf4jApiPresent set to true at build time
Field org.springframework.core.NativeDetector#imageCode set to true at build time
Field org.springframework.format.support.DefaultFormattingConversionService#jsr354Present set to false at build time
Field org.springframework.core.KotlinDetector#kotlinPresent set to false at build time
Field org.springframework.core.KotlinDetector#kotlinReflectPresent set to false at build time
Field org.springframework.cglib.core.AbstractClassGenerator#imageCode set to true at build time
Field org.springframework.boot.logging.log4j2.Log4J2LoggingSystem$Factory#PRESENT set to false at build time
Field org.springframework.boot.logging.java.JavaLoggingSystem$Factory#PRESENT set to true at build time
Field org.springframework.boot.logging.logback.LogbackLoggingSystem$Factory#PRESENT set to true at build time
Field org.springframework.boot.logging.logback.LogbackLoggingSystemProperties#JBOSS_LOGGING_PRESENT set to false at build time
Field org.springframework.context.event.ApplicationListenerMethodAdapter#reactiveStreamsPresent set to false at build time
[2/7] Performing analysis...  [*******]                                                                 (60.8s @ 2.15GB)
   8,903 (88.31%) of 10,082 classes reachable
  13,147 (64.27%) of 20,456 fields reachable
  40,485 (56.88%) of 71,181 methods reachable
     365 classes,   115 fields, and 1,191 methods registered for reflection
      64 classes,    70 fields, and    55 methods registered for JNI access
       4 native libraries: dl, pthread, rt, z
[3/7] Building universe...                                                                               (8.9s @ 2.03GB)
[4/7] Parsing methods...      [***]                                                                      (9.6s @ 0.82GB)
[5/7] Inlining methods...     [***]                                                                      (3.7s @ 2.16GB)
[6/7] Compiling methods...    [*******]                                                                 (48.2s @ 1.97GB)
[7/7] Creating image...                                                                                  (5.6s @ 1.60GB)
  17.53MB (49.48%) for code area:    25,460 compilation units
  17.60MB (49.66%) for image heap:  215,829 objects and 25 resources
 312.40KB ( 0.86%) for other data
  35.44MB in total
------------------------------------------------------------------------------------------------------------------------
Top 10 packages in code area:                               Top 10 object types in image heap:
 936.87KB java.util                                            3.72MB byte[] for code metadata
 594.27KB java.lang.invoke                                     2.07MB java.lang.String
 469.21KB c.s.org.apache.xerces.internal.impl.xs.traversers    2.06MB java.lang.Class
 455.78KB java.lang                                            1.68MB byte[] for general heap data
 423.05KB com.sun.org.apache.xerces.internal.impl              1.60MB byte[] for java.lang.String
 407.79KB com.sun.crypto.provider                            765.10KB com.oracle.svm.core.hub.DynamicHubCompanion
 375.00KB org.springframework.beans.factory.support          576.38KB java.util.HashMap$Node
 371.71KB java.io                                            513.09KB int[][]
 354.25KB java.util.concurrent                               394.88KB java.lang.String[]
 328.57KB java.text                                          394.65KB byte[] for reflection metadata
  12.74MB for 405 more packages                                3.23MB for 1771 more object types
------------------------------------------------------------------------------------------------------------------------
                        12.0s (8.0% of total time) in 35 GCs | Peak RSS: 3.04GB | CPU load: 5.08
------------------------------------------------------------------------------------------------------------------------
Produced artifacts:
 /app/target/demo (executable)
 /app/target/demo.build_artifacts.txt (txt)
========================================================================================================================
Finished generating 'demo' in 2m 29s.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  03:11 min
[INFO] Finished at: 2023-01-27T18:05:23Z
[INFO] ------------------------------------------------------------------------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM