Static Native Images

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.

Prerequisites

## Build a Static Native Image

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

Build a Mostly Static Native Image

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

Building musl

Building zlib

Getting libstdc++

libstdc++ is obtained by building gcc. There are multiple approaches to obtaining it:

  1. Build gcc with musl-gcc.
  2. 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 you 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.

  3. Take libstdc++.a from Alpine. In each case, libstdc++.a must be placed in ${RESULT_DIR}/lib.