Sub topic 11
Static linking and container images
This chapter assumes you can build a native executable with native-image (covered earlier in this tutorial). It also assumes you have used a basic multi-stage Dockerfile. Here you will decide how much of the C library your executable carries, and how to put it in a small container image.
What the executable still depends on
A native executable does not need a JVM. By default it still links dynamically against system libraries on Linux, such as libc and libz. If the target machine lacks them, the program fails at startup with a loader error. You can see the dependencies with ldd:
ldd helloNative Image offers three linking modes on Linux:
| Mode | Option | Libc | Typical base image |
|---|---|---|---|
| Dynamic (default) | none | Shared glibc from the host | A distribution with glibc, or a distroless `base` image |
| Mostly static | `--static-nolibc` | Shared libc, other libraries static | Distroless `base` image |
| Fully static with glibc | `--static` | glibc linked in | `scratch` or distroless `static` |
| Fully static with musl | `--static --libc=musl` | musl linked in | `scratch` |
Static builds need static versions of the system libraries installed on the build machine. The musl option also needs a musl toolchain. Availability of --static-nolibc and of musl on each CPU architecture depends on the GraalVM release, so check the documentation for your version.
Building a static executable
Pass the options to native-image as usual:
javac Hello.java
native-image --static --libc=musl Hello
ldd helloFor a static binary, ldd reports that the file is not a dynamic executable. That result confirms no shared libraries are needed.
Pitfall: a binary that is static but uses glibc features such as name-service lookups (getaddrinfo) can behave differently from a dynamic one. Musl avoids that class of problem because it is linked in completely. Test DNS and any native libraries your application uses.
A multi-stage Dockerfile
You cannot cross-compile with Native Image. The executable must be built on the same operating system and CPU architecture as the target. A multi-stage build handles this. The first stage has the compiler and toolchain, and the last stage holds only the executable.
FROM ghcr.io/graalvm/native-image-community:21-muslib AS build
WORKDIR /app
COPY Hello.java .
RUN javac Hello.java && native-image --static --libc=musl Hello
FROM scratch
COPY --from=build /app/hello /hello
ENTRYPOINT ["/hello"]The final image contains one file. It has no shell, no package manager and no libraries, so it is small and has little attack surface. The cost is that you cannot docker exec into it for debugging.
If your application needs CA certificates, time zone data, or a non-root user, scratch is too bare. In that case, copy those files from the build stage, or use a distroless image. Distroless base images ship glibc, certificates and time zone data, which suits dynamic and mostly static builds.
FROM ghcr.io/graalvm/native-image-community:21 AS build
WORKDIR /app
COPY Hello.java .
RUN javac Hello.java && native-image --static-nolibc Hello
FROM gcr.io/distroless/base-debian12
COPY --from=build /app/hello /hello
ENTRYPOINT ["/hello"]Letting the build tool do it
Frameworks can produce the image for you. With Spring Boot, a Cloud Native Buildpacks build creates a container without a hand-written Dockerfile:
mvn -Pnative spring-boot:build-imageQuarkus and Micronaut offer similar container-build options. Their native image builds also run inside a container when no local GraalVM is installed. Those details are covered in the framework chapter.
Common errors
cannot find -lzor similar linker errors during--static: install the static library packages (for examplezlib-static) on the build machine, or use the musl-enabled builder image.exec format errorwhen the container starts: the image was built for a different CPU architecture than the host, such as an ARM build on an x86 machine.no such file or directoryfor a file that exists: a dynamic executable is missing its loader or libc in the final image. Use a base image with glibc, or build statically.
Exercise
Your application is built with the default native-image command and runs correctly in the build container. In a FROM scratch image it fails at startup with "no such file or directory" for /app. Why does this happen, and what are two ways to fix it?
Show answer
The executable is dynamically linked, and scratch contains no dynamic loader or libc, so the kernel cannot start it. One fix is to rebuild it fully static with --static --libc=musl. The other is to keep the dynamic build and switch the final image to one that provides glibc, such as a distroless base image. A mostly static build with --static-nolibc also works on that base image.