Chapter 11. Producing a native executable

You can produce a native executable from your Quarkus application using a container runtime such as Podman or Docker. Quarkus produces a binary executable using a builder image, which you can use together with the Red Hat Universal Base Images RHEL8-UBI and RHEL8-UBI minimal. Red Hat build of Quarkus 1.7 uses registry.access.redhat.com/quarkus/mandrel-20-rhel8:20.3 as a default for the quarkus.native.builder-image property.

The native executable for your application contains the application code, required libraries, Java APIs, and a reduced version of a virtual machine (VM). The smaller VM base improves the startup time of the application and produces a minimal disk footprint.

Procedure

  1. Open the Getting Started project pom.xml file and verify that it includes the native profile:

    <profiles>
        <profile>
            <id>native</id>
            <properties>
                <quarkus.package.type>native</quarkus.package.type>
            </properties>
        </profile>
    </profiles>
    Note

    Using Quarkus native profile allows you to run both the native executable and the native image tests.

  2. Build a native executable using one of the following methods:

    1. Build a native executable with Docker:

      ./mvnw package -Pnative -Dquarkus.native.container-build=true
    2. Build a native executable with Podman:

      ./mvnw package -Pnative -Dquarkus.native.container-build=true -Dquarkus.native.container-runtime=podman

      These commands create the getting-started-*-runner binary in the target directory.

      Important

      Compiling a Quarkus application to a native executable consumes a lot of memory during analysis and optimization. You can limit the amount of memory used during native compilation by setting the quarkus.native.native-image-xmx configuration property. Setting low memory limits might increase the build time.

  3. Run the native executable:

    ./target/getting-started-*-runner

    When you build the native executable the prod profile is enabled and the Quarkus native tests run using the prod profile. You can change this using the quarkus.test.native-image-profile property.

11.1. Creating a container manually

This section shows you how to manually create a container image with your application for Linux X86_64. When you produce a native image using the Quarkus Native container it creates an executable that targets the Linux X86_64 operating system. If your host operating system is different from this, you will not be able to run the binary directly and you will need to create a container manually.

Your Quarkus Getting Started project includes a Dockerfile.native in the src/main/docker directory with the following content:

FROM registry.access.redhat.com/ubi8/ubi-minimal
WORKDIR /work/
COPY target/*-runner /work/application
RUN chmod 775 /work
EXPOSE 8080
CMD ["./application", "-Dquarkus.http.host=0.0.0.0"]
Universal Base Image (UBI)

The Dockerfiles use UBI as a base image. This base image was designed to work in containers. The Dockerfiles use the minimal version of the base image to reduce the size of the produced image.

Procedure

  1. Build a native Linux executable using one of the following methods:

    1. Build a native executable with Docker:

      ./mvnw package -Pnative -Dquarkus.native.container-build=true
    2. Build a native executable with Podman:

      ./mvnw package -Pnative -Dquarkus.native.container-build=true -Dquarkus.native.container-runtime=podman
  2. Build the container image using one of the following methods:

    1. Build the container image with Docker:

      docker build -f src/main/docker/Dockerfile.native -t quarkus-quickstart/getting-started .
    2. Build the container image with Podman

      podman build -f src/main/docker/Dockerfile.native -t quarkus-quickstart/getting-started .
  3. Run the container:

    1. Run the container with Docker:

      docker run -i --rm -p 8080:8080 quarkus-quickstart/getting-started
    2. Run the container with Podman:

      podman run -i --rm -p 8080:8080 quarkus-quickstart/getting-started

For information about deploying Quarkus Maven applications on Red Hat OpenShift Container Platform, see Deploying your Quarkus applications on Red Hat OpenShift Container Platform.