Skip to main content

Static and Mostly Static Images

Native Image can create static native images and mostly static native images.

Static Images

Static native images are statically linked binaries without additional library dependencies. These images can run on slim or distroless container images. They are created by statically linking against a libc implementation.

--libc specifies the libc implementation to use when building static images.

ValuesDescription
glibcThe GNU C Library
muslmusl libc
bionicbionic
Create a static image
native-image --static --libc=glibc Main

Mostly Static Images

Mostly static native images statically link everything except libc. This only works for glibc.

Create a mostly static image
native-image -H:+StaticExecutableWithDynamicLibC Main

Container Image

The code below shows the Dockerfile to build container images for a static image and a mostly static image.

  • The container image for the static image uses the base image scratch. The result container image is only 16MB.
  • The container image for the mostly static image requires a base image that includes glibc. The size of result container image is much larger.
FROM ghcr.io/graalvm/graalvm-ce:java17-21.3 AS builder

RUN gu install native-image

RUN mkdir /build && mkdir /build/source && mkdir /build/target

WORKDIR /build

COPY Main.java ./source

RUN javac ./source/Main.java -d ./target

RUN native-image -cp ./target --install-exit-handlers --static --libc=glibc -H:Name=helloworld Main

#############

FROM scratch

WORKDIR /

COPY --from=builder /build/helloworld /helloworld

ENTRYPOINT ["/helloworld"]

If a base image of Linux distribution has to be used for other reasons, a mostly static image is a better choice. Otherwise, a static image with scratch base image can save a lot of space.