Static native images are statically linked binaries which can be used without any additional library dependencies. This makes them suitable for use in a Docker container.
- Right now, this only works on Linux AMD64 on Java 11.
- You will need
gcc
,make
, andconfigure
. - Create a directory that will hold the libraries you build. You will refer to this directory as
${RESULT_DIR}
. - Download the latest
musl
release here. This document will usemusl-1.2.0
. - Download the latest
zlib
release here. This document will usezlib-1.2.11
.
If you have musl-gcc
on the path, you can build a native image statically linked against muslc
with the following options: --static --libc=musl
.
To verify that musl-gcc
is on the path, run musl-gcc -v
.
To build a static native image, use:
native-image --static --libc=musl [other arguments] Class
As of GraalVM version 20.2, you can build a “mostly static” native image which link statically everything except libc
. Native images built this way are convenient to run in Docker containers, for example, based on
distroless minimal Linux, glibc-based systems.
To build a mostly-static native image native image, use:
native-image -H:+StaticExecutableWithDynamicLibC [other arguments] Class
- Extract the musl release
tarball
andcd
into the extracted directory. - Run
./configure --disable-shared --prefix=${RESULT_DIR}
. - Run
make
. - Run
make install
. Other than buildingmusl
libraries, the build also creates agcc
wrapper calledmusl-gcc
in the${RESULT_DIR}/bin
directory. You should now put this wrapper on yourPATH
by runningexport PATH=$PATH:${RESULT_DIR}/bin
.
- Extract the zlib release
tarball
andcd
into the extracted directory. - You need to compile zlib and link it against musl so set
CC
tomusl-gcc
:export CC=musl-gcc
. - Run
./configure --static --prefix=${RESULT_DIR}
. - Run
make
. - Run
make install
.
libstdc++
is obtained by building gcc. There are multiple approaches to obtaining it:
- Build gcc with
musl-gcc
. - Use
libstdc++.a
from your distribution. If you choose this path, check the FAQs page, "How do I use the musl-gcc wrapper?":
The existing libstdc++ is actually compatible with musl in most cases and could be used by copying it into the musl library path, but the C++ header files are usually not compatible. Since do not need C++ header files, this approach should work. If you run into issues, make sure they are not caused by your ditribution's
libstdc++.a
.
- Take
libstdc++.a
from Alpine. In each case,libstdc++.a
must be placed in${RESULT_DIR}/lib
.