Sub topic 11
Static linking and container images
This chapter covers how a native executable links against system libraries, and how to choose a base image when you put one in a container. It assumes you can build a native image with native-image or a Maven or Gradle plugin, and that you know basic Docker: multi-stage builds, FROM, COPY and ENTRYPOINT. You should also know what a shared library (.so) is.
What the executable still needs
A native executable contains your compiled code, the parts of the JDK that static analysis found reachable, and the Substrate VM runtime (garbage collector, thread support, and so on). It does not contain a JVM. It still calls the operating system's C library for things like file I/O, sockets, threads and memory mapping.
By default, native-image links the executable dynamically. The result needs these at run time:
- The C library (
glibcon most Linux distributions). - A few other shared libraries, such as
libzfor compression. - The same or a newer glibc version than the one used at build time.
You can check this on Linux:
native-image -jar app.jar -o app
ldd appldd lists entries such as libc.so.6, libz.so.1 and libpthread.so.0. Those entries are the runtime requirements your container image or host must satisfy.
Three linking modes
Native Image on Linux offers three levels of static linking.
| Mode | Option | What is linked statically | Runtime needs |
|---|---|---|---|
| Dynamic (default) | none | Only your code and the VM runtime | glibc and a few shared libraries |
| Mostly static | `--static-nolibc` | Everything except the C library | A compatible glibc |
| Fully static with musl | `--static --libc=musl` | Everything, including the C library | Nothing |
Older releases enabled the mostly static mode with -H:+StaticExecutableWithDynamicLibC. Check the documentation for your GraalVM version before you copy flags from an older article.
Mostly static
The mostly static mode removes the dependency on libz and similar libraries, but the executable still loads glibc dynamically. This is a good fit for distroless images that ship glibc and little else.
native-image --static-nolibc -jar app.jar -o appThe build machine needs static versions of those libraries (for example libz.a). If the linker reports a missing -lz, install your distribution's static zlib development package.
Fully static with musl
musl is a small C library designed for static linking. With it, the executable has no runtime dependencies, so you can run it in an image with no operating system files at all.
native-image --static --libc=musl -jar app.jar -o app
ldd appFor a fully static executable, ldd prints "not a dynamic executable". The file command shows "statically linked" as well.
The build has requirements of its own:
- A musl toolchain must be installed, including a
musl-gccwrapper. - zlib must be built against musl and installed where the linker finds it.
- Musl support exists for Linux on x64. Support for other architectures depends on your GraalVM release, so check its documentation.
Some GraalVM container images and release bundles already include the musl toolchain, which saves you the manual setup.
Why not a static glibc build?
You can pass --static without --libc=musl, and the executable then links glibc statically. That is discouraged. Parts of glibc, notably name resolution through getaddrinfo and the Name Service Switch, load shared libraries at run time even in a static executable. A "static" glibc binary can therefore fail on a machine with a different glibc version, and the failure appears only when the application resolves a host name.
Differences to expect with musl
Musl is not a drop-in replacement for glibc. Two differences matter in practice:
- Its DNS resolver does not use
nsswitch.conf, so name lookup can behave differently from the same code on glibc. - Behavior around locales and some less common system calls differs.
Test networking in the final image, not only on your development machine.
Platform limits
- Fully static executables are a Linux feature. macOS does not support fully static linking, and Windows has its own runtime model.
- A native executable targets the operating system and CPU architecture of the machine that built it. You cannot build a Linux executable on macOS. To build for Linux from another OS, build inside a Linux container.
- By default, the compiler may emit instructions for your build machine's CPU. If the build and deployment CPUs differ, an executable can crash with an illegal instruction error. Recent GraalVM releases provide the
-marchoption, for example-march=compatibility, to target a conservative baseline. Check that your release supports it.
Choosing a container base image
The linking mode decides how small the image can be.
flowchart TDThe diagram below follows the decision.
Here is a comparison of the three choices.
| Base image | Typical use | Advantages | Costs |
|---|---|---|---|
| `scratch` | Fully static musl executable | Smallest image, almost no attack surface | No shell, no CA certificates, no `/etc/passwd`, hard to debug |
| Distroless (`gcr.io/distroless/base-debian12`) | Mostly static or dynamic with glibc | Small, includes glibc and CA certificates, runs as non-root | No shell by default |
| Slim distribution (`debian:bookworm-slim`) | Dynamic default build | Has a shell and package manager, easy to debug | Larger, more packages to patch |
A multi-stage build
A multi-stage Dockerfile builds the executable in an image with the GraalVM toolchain and copies only the result into a small runtime image. The JDK and the compiler never reach production.
FROM ghcr.io/graalvm/native-image-community:21 AS builder
WORKDIR /build
COPY target/app.jar app.jar
RUN native-image -jar app.jar -o app
FROM debian:bookworm-slim
RUN useradd --system --no-create-home appuser
COPY --from=builder /build/app /usr/local/bin/app
USER appuser
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/app"]Build and run it with:
docker build -t app-native .
docker run --rm -p 8080:8080 app-nativeThe builder image is based on a recent Oracle Linux, and the runtime image is Debian. The build still works because the Debian image carries a glibc at least as new as the builder's. The reverse, building on a new distribution and running on an old one, produces errors such as version 'GLIBC_2.34' not found.
The build stage takes a lot of memory and time, often several gigabytes of RAM and a minute or more. Give the Docker daemon enough memory, or the build is killed with no clear message. Exit code 137 is the usual sign.
Moving to scratch
For a fully static executable, the runtime stage can be empty:
FROM scratch
COPY --from=builder /build/app /app
ENTRYPOINT ["/app"]This only works if the builder stage ran native-image --static --libc=musl and the toolchain was present. A scratch image has none of the following, and you must add each one you need:
- CA certificates for outgoing HTTPS. Copy a bundle such as
/etc/ssl/certs/ca-certificates.crtfrom another stage. - User and group files, if you want
USERto name a non-root user. You can use a numeric user ID instead, such asUSER 10001. - Timezone data, if your application loads zone files from the operating system.
- Any file the application opens by path.
Image size and startup
A native executable is often tens of megabytes. Its size comes from the reachable JDK code, your dependencies and the embedded image heap. The container image then adds the base layer, which ranges from a few megabytes for distroless to over a hundred for a full distribution. Compared with a JVM image, which carries a JDK of several hundred megabytes, a native image is usually much smaller, and it starts in milliseconds.
Some options that reduce size:
- Remove unused dependencies, because every reachable class is compiled in.
- Use the size optimization level
-Oswhen startup and footprint matter more than peak speed. - Choose a base image with fewer packages.
Build servers and reproducibility
Builds in CI should run inside the same builder image you use locally. This keeps the GraalVM version, the glibc version and the toolchain constant. Pin the image tag to a specific version, not a moving tag such as latest, so an upgrade does not change your executable by surprise.
Framework tooling can do this for you. For example, Spring Boot can build a container image with Cloud Native Buildpacks, and Quarkus can run the native build inside a container. Those options are covered in the chapter on framework support. They choose the base image and linking mode for you, so check what they produce before you rely on the defaults.
Common problems
cannot find -lzor-lcduring linking. The static libraries are missing on the build machine. Install the static zlib and libc development packages, or use a builder image that includes them.musl-gccnot found. The musl toolchain is not installed or not on thePATH.GLIBC_x.y not foundat run time. The runtime image's glibc is older than the build image's. Use a newer runtime base or an older builder.exec format error. The executable was built for a different CPU architecture than the one running the container. This often happens when you build on an Apple silicon laptop and deploy to x64.no such file or directoryfor an executable that exists. A dynamic executable is missing its loader or libraries in the image. This is typical of copying a glibc build intoscratch.- TLS errors in
scratch. There is no CA bundle. Copy one in.
Exercise
You build with native-image --static --libc=musl -jar app.jar -o app and copy app into a scratch image. The container starts, but every outgoing HTTPS call fails with a certificate error. What is the likely cause, and what would you change?
Show answer
The scratch image has no CA certificate bundle, so the application has no trusted roots for verifying servers. Copy a CA bundle, for example /etc/ssl/certs/ca-certificates.crt, from another build stage into the final image, or switch to a distroless base image that already includes certificates. Then test an HTTPS call inside the final image.
Exercise
A native executable built with the default settings on a recent Linux distribution works on your laptop but fails in a container with GLIBC_2.34 not found. Name two ways to fix it.
Show answer
The container's glibc is older than the one used for the build. Either run the container on a base image with a glibc at least as new as the builder's, or build in an older or matching builder image. A third option is a fully static musl build, which removes the glibc dependency, but then you must test networking and other behavior that differs from glibc.
Exercise
How can you confirm from the command line that an executable is fully static, and why is --static without --libc=musl discouraged?
Show answer
Run ldd app, which prints "not a dynamic executable" for a fully static binary, or run file app and look for "statically linked". A static glibc build is discouraged because glibc still loads shared libraries at run time for name resolution and similar features, so the binary can fail on systems with a different glibc.