As we have seen, the Quarkus Maven plugin has also produced src/main/docker/Dockerfile.native, which can be used as a template so that we can run our native executable in a container. Here's the content of this file:
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"]
Since there's no need to use a JDK layer to start our application, the base layer for our container will be a stripped-down RHEL image known as ubi-minimal.
Before building the Docker image, package your application by including the -Dnative-image.docker-build option:
$ mvn package -Pnative -Dnative-image.docker-build=true
Check that the build was successful and then build the image with the following command:
$ docker build -f src/main/docker/Dockerfile.native -t quarkus/hello-okd-native .
From the console, you will see that the container will be created in much the same way that the Java application was, but using a different initial image (ubi-minimal):
Sending build context to Docker daemon 32.57 MB
Step 1/6 : FROM registry.access.redhat.com/ubi8/ubi-minimal
---> 8c980b20fbaa
Step 2/6 : WORKDIR /work/
---> Using cache
---> 0886c0b19e07
Step 3/6 : COPY target/*-runner /work/application
---> 7e66ae6447ce
Removing intermediate container 2ddc91992af5
Step 4/6 : RUN chmod 775 /work
---> Running in e8d6ffbbc14e
---> 780f6562417d
Removing intermediate container e8d6ffbbc14e
Step 5/6 : EXPOSE 8080
---> Running in d0d48475565f
---> 554f79b4cbb2
Removing intermediate container d0d48475565f
Step 6/6 : CMD ./application -Dquarkus.http.host=0.0.0.0
---> Running in e0206ff3971f
---> 33021bdaf4a4
Removing intermediate container e0206ff3971f
Successfully built 33021bdaf4a4
Let's check that the image is available in the Docker repository:
$ docker images | grep hello-okd-native
You should see the following output:
quarkus/hello-okd-native latest 33021bdaf4a4 59 seconds ago 113 MB
The quarkus/hello-okd-native image is now available. Now, run the container image using the following command:
$ docker run -i --rm -p 8080:8080 quarkus/hello-okd-native
No additional JVM layers will be displayed on the console. Here, we can see that our service was started up in just a few milliseconds:
2019-11-11 11:59:46,817 INFO [io.quarkus] (main) hello-okd 1.0-SNAPSHOT (running on Quarkus 1.0.0.CR1) started in 0.005s. Listening on: http://0.0.0.0:8080
2019-11-11 11:59:46,817 INFO [io.quarkus] (main) Profile prod activated.
2019-11-11 11:59:46,817 INFO [io.quarkus] (main) Installed features: [cdi, resteasy]y]
Verify that the application returns the container ID when requesting the getContainerId URI:
curl http://localhost:8080/getContainerId
In our case, the output is as follows:
You are running on ff6574695d68
Great! We just managed to run a native application as a Docker image. Our next task will be deploying our image into a Kubernetes-native environment.