使用 Docker 创建 GraalVM 原生镜像

Create GraalVM native image with dockers

我正在尝试使用 Dockers 创建 GraalVM 本机映像。我已经创建了一个 Micronaut 项目,并成功创建了一个 jar 应用程序,并 运行 将其放入 docker;我还用这个 jar 文件创建了一个 GraalVM 本机映像,现在可以 运行 这个应用程序,但我需要 运行 docker 中的 graalvm 本机映像,寻找答案在论坛上,我发现有必要在 docker 中构建本机映像,所以我尝试使用此 Dockerfile:

# use graalvm image
# replace the occurences of "product" with you jar name
FROM openjdk:8u171-alpine3.7

# expose your port, 8080 fo Micronaut applicatoin
EXPOSE 8080

# copy the fat jar
COPY build/libs/*-all.jar products.jar

# create the reflection configuration file generated by Micronaut
# for other frameworks you need to construct the file yourself
ADD . build
#RUN java -cp products.jar io.micronaut.graal.reflect.GraalClassLoadingAnalyzer

# run the native image compiler, you should already know the command arguments
# if you're not running micronaut
ENV PATH=PATHTOGRAALVM/graalvm-ce-java8-20.3.0/bin:$PATH
ENV JAVA_HOME=PATHTOGRAALVM/graalvm-ce-java8-20.3.0

CMD native-image --no-server \
             --class-path products.jar \
         -H:ReflectionConfigurationFiles=build/reflect.json \
         -H:EnableURLProtocols=http \
         -H:IncludeResources="logback.xml|application.yml|META-INF/services/*.*" \
         -H:Name=products \
         -H:Class=products.Application \
         -H:+ReportUnsupportedElementsAtRuntime \
         -H:+AllowVMInspection \
         --rerun-class-initialization-at-runtime='sun.security.jca.JCAUtil$CachedSecureRandomHolder,javax.net.ssl.SSLContext' \
         --delay-class-initialization-to-runtime=io.netty.handler.codec.http.HttpObjectEncoder,io.netty.handler.codec.http.websocketx.WebSocket00FrameEncoder,io.netty.handler.ssl.util.ThreadLocalInsecureRandom

# the native command will be used to run the application
CMD ./products

它不会抛出任何异常或其他东西,所以我尝试 运行 我的 docker 用这个:

docker build --tag="products-with-graavm" . 
docker run -d -p 8080:8080 products-with-graavm

但它什么也没做,我的应用程序不是 运行ning,我的 Dockerfile 有问题还是我需要添加其他配置来创建我的本机映像?谢谢

我已经在 docker 中创建了我的原生图像,我在我的原生图像命令行中删除了一些在创建原生图像时抛出异常的行。这几行试图找到一些不在那个地方的文件。此外,我删除了一些来自旧版本 GraalVM 的行。这些行是这样的:

-H:ReflectionConfigurationFiles=build/reflect.json \
-H:+AllowVMInspection \
--rerun-class-initialization-at-runtime='sun.security.jca.JCAUtil$CachedSecureRandomHolder,javax.net.ssl.SSLContext' \
--delay-class-initialization-to-runtime=io.netty.handler.codec.http.HttpObjectEncoder,io.netty.handler.codec.http.websocketx.WebSocket00FrameEncoder,io.netty.handler.ssl.util.ThreadLocalInsecureRandom

谢谢。