My native images are building well so now I am experimenting with other areas - specifically building a static image.
Per instructions on the internet I've added --static -H:UseMuslC=bundle/ arguments to my native image build but I get the following error:
Error: Could not find option 'UseMuslC'. Use -H:PrintFlags= to list all available options.
Error: Use -H:+ReportExceptionStackTraces to print stacktrace of underlying exception
Error: Image build request failed with exit status 1
com.oracle.svm.driver.NativeImage$NativeImageError: Image build request failed with exit status 1
at com.oracle.svm.driver.NativeImage.showError(NativeImage.java:1558)
at com.oracle.svm.driver.NativeImage.build(NativeImage.java:1308)
at com.oracle.svm.driver.NativeImage.performBuild(NativeImage.java:1269)
at com.oracle.svm.driver.NativeImage.main(NativeImage.java:1228)
at com.oracle.svm.driver.NativeImage$JDK9Plus.main(NativeImage.java:1740)
For reference my Dockerfile looks like:
FROM oracle/graalvm-ce:20.2.0-java11 as build
RUN gu install native-image
RUN curl -L -o musl.tar.gz https://github.com/gradinac/musl-bundle-example/releases/download/v1.0/musl.tar.gz && \
tar -xvzf musl.tar.gz
COPY target/myjar.jar .
COPY graal ./graal
RUN native-image \
--no-server \
--static \
-H:UseMuslC=bundle/ \
-J-Xmx10G \
--no-fallback \
--verbose \
--enable-all-security-services \
--enable-url-protocols=http,https \
-H:ReflectionConfigurationFiles=graal/reflect-config.json \
-H:ResourceConfigurationFiles=graal/resource-config.json \
-H:DynamicProxyConfigurationFiles=graal/proxy-config.json \
-H:+ReportExceptionStackTraces \
--report-unsupported-elements-at-runtime \
--allow-incomplete-classpath \
-H:IncludeResources=".*" \
-H:+TraceClassInitialization \
-H:+PrintClassInitialization \
--install-exit-handlers \
-H:+AddAllCharsets \
-H:+RemoveSaturatedTypeFlows \
-H:IncludeResourceBundles=org.apache.xmlbeans.impl.regex.message,com.sun.org.apache.xml.internal.serializer.utils.SerializerMessages \
-jar myjar.jar
Hello @emrul,
On GraalVM 20.1 you were able to use the flag -H:UseMuslC. But GraalVM 20.2 uses a different approach. Please see this document that explains the process:
https://github.com/oracle/graal/blob/master/substratevm/StaticImages.md
Ah, @fernando-valdez thank you for pointing me to that!
Ok, so I've managed to get it to work without much hassle in Docker (no kidding this has been the smoothest part of my Native Image exeperience so far!).
For anyone else who wants a copy + paste for building static images, here is my Dockerfile:
# Note: Without JAR prefix (e.g. If given 'myjar', will append '.jar')
ARG SOURCE_FILE="myjar"
FROM oracle/graalvm-ce:20.2.0-java11 as build
RUN gu install native-image
# BEGIN PRE-REQUISITES FOR STATIC NATIVE IMAGES FOR GRAAL 20.2.0
# SEE: https://github.com/oracle/graal/blob/master/substratevm/StaticImages.md
ARG RESULT_LIB="/staticlibs"
RUN mkdir ${RESULT_LIB} && \
curl -L -o musl.tar.gz https://musl.libc.org/releases/musl-1.2.1.tar.gz && \
mkdir musl && tar -xvzf musl.tar.gz -C musl --strip-components 1 && cd musl && \
./configure --disable-shared --prefix=${RESULT_LIB} && \
make && make install && \
cd / && rm -rf /muscl && rm -f /musl.tar.gz && \
cp /usr/lib/gcc/x86_64-redhat-linux/4.8.2/libstdc++.a ${RESULT_LIB}/lib/
ENV PATH="$PATH:${RESULT_LIB}/bin"
ENV CC="musl-gcc"
RUN curl -L -o zlib.tar.gz https://zlib.net/zlib-1.2.11.tar.gz && \
mkdir zlib && tar -xvzf zlib.tar.gz -C zlib --strip-components 1 && cd zlib && \
./configure --static --prefix=${RESULT_LIB} && \
make && make install && \
cd / && rm -rf /zlib && rm -f /zlib.tar.gz
#END PRE-REQUISITES FOR STATIC NATIVE IMAGES FOR GRAAL 20.2.0
ARG SOURCE_JAR_DIR="target/"
ARG SOURCE_FILE
ARG GRAAL_CONFIG_DIR="graal"
COPY ${SOURCE_JAR_DIR}${SOURCE_JAR} .
COPY ${GRAAL_CONFIG_DIR} ./graal
RUN native-image \
--no-server \
--static \
--libc=musl \
-J-Xmx10G \
--no-fallback \
--verbose \
--enable-all-security-services \
--enable-url-protocols=http,https \
-H:ReflectionConfigurationFiles=graal/reflect-config.json \
-H:ResourceConfigurationFiles=graal/resource-config.json \
-H:DynamicProxyConfigurationFiles=graal/proxy-config.json \
-H:+ReportExceptionStackTraces \
--report-unsupported-elements-at-runtime \
--allow-incomplete-classpath \
-H:IncludeResources=".*" \
-H:+TraceClassInitialization \
-H:+PrintClassInitialization \
--install-exit-handlers \
-H:+AddAllCharsets \
-H:IncludeResourceBundles=org.apache.xmlbeans.impl.regex.message,com.sun.org.apache.xml.internal.serializer.utils.SerializerMessages \
-jar ${SOURCE_FILE}.jar
Most helpful comment
Ah, @fernando-valdez thank you for pointing me to that!
Ok, so I've managed to get it to work without much hassle in Docker (no kidding this has been the smoothest part of my Native Image exeperience so far!).
For anyone else who wants a copy + paste for building static images, here is my Dockerfile: