$show=/label

Quarkus (Native Image + Docker Deploy OpenShift)

SHARE:

Learn about QuarkusIO and if it's an alternative for bringing Java more effectively to the cloud. Live hot reloaded changes example, creating native docker and deploy the image to openshift example.

1. Introduction


In this article, you will learn a new hot technology Quarkus IO how it became alternatively to bring the java applications to the cloud.

You will learn
  • Creating Quarkus Application with Quarkus init.
  • Auto Hot Reload Chances feature
  • Build and Start the container
  • Building Docker container
  • Deploying docker image to Openshift.


This gives more clear to use where the instances are created and destroyed frequently. And also boot time and serving the first request gives a much better user experience.

Always, Python and javascript are coming in the first place to deploy the apps into the cloud. But, let us see how to use Quarkus to deploy the apps into the cloud.

Guide to QuarkusIO (Native Image + Docker Deploy OpenShift)


Spring Boot Cloud Related Books

Guide to QuarkusIO


2. QuarkusIO


Quarkus always gives the less boot time and servers the first request very fast as compare to Spring Boot 2.0 applications.

It is integrated with GraalVM that makes Quarkus will come ahead of time (AOT).

This is built to support all frameworks along with support for all extensions such as JAX-RS, CDI,  Web, ORM, and Messages including Hibernate, JMS, Kafka, OpenShift, Kubernetes and vert.x for auto-reload changes.

3. Quarkus First Hello World Application


You can create a maven application with the below command.

[mvn io.quarkus:quarkus-maven-plugin:0.13.1:create \
    -DprojectGroupId=com. javaprogramto.quarkus \
    -DprojectArtifactId=quarkus-project \
    -DclassName="com.javaprogramto.quarkus.HelloResource" \
    -Dpath="/hello"]

Or You can create from https://code.quarkus.io/

https://code.quarkus.io/


Provide all the information such as artifact id and add the dependencies.. here we have added Kubernetes also to see the default files.

4. Quarkus Project Structure


The following is the default project structure with JAX-RS and Kubernates.

It has created dockerfile for JVM and native.

MacBook-Pro-2:code-with-quarkus$ tree
.
├── README.md
├── code-with-quarkus.iml
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
    ├── main
    │   ├── docker
    │   │   ├── Dockerfile.jvm
    │   │   └── Dockerfile.native
    │   ├── java
    │   │   └── org
    │   │       └── javaprogramto
    │   │           └── quarkus
    │   │               └── ExampleResource.java
    │   └── resources
    │       ├── META-INF
    │       │   └── resources
    │       │       └── index.html
    │       └── application.properties
    └── test
        └── java
            └── org
                └── javaprogramto
                    └── quarkus
                        ├── ExampleResourceTest.java
                        └── NativeExampleResourceIT.java

15 directories, 12 files

5. Default Rest API Example in Quarkus


This is the one created with the example demo and "hello" endpoint will return response as "hello world".

package org.javaprogramto.quarkus;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class ExampleResource {

    @GET    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "hello world";
    }
}

Below Two files you can ignore for now and just showing the contents. If you are interested and have some idea about docker, Please go through it.

Otherwise, Directly jump into section 6l

Dockerfile.native:

####
# This Dockerfile is used in order to build a container that runs the Quarkus application in native (no JVM) mode
#
# Before building the docker image run:
#
# mvn package -Pnative -Dquarkus.native.container-build=true
#
# Then, build the image with:
#
# docker build -f src/main/docker/Dockerfile.native -t quarkus/code-with-quarkus .
#
# Then run the container using:
#
# docker run -i --rm -p 8080:8080 quarkus/code-with-quarkus
#
###
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.1
WORKDIR /work/
COPY target/*-runner /work/application

# set up permissions for user `1001`
RUN chmod 775 /work /work/application \
  && chown -R 1001 /work \
  && chmod -R "g+rwX" /work \
  && chown -R 1001:root /work

EXPOSE 8080
USER 1001

CMD ["./application", "-Dquarkus.http.host=0.0.0.0"]

Dockerfile.jvm

####
# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode
#
# Before building the docker image run:
#
# mvn package
#
# Then, build the image with:
#
# docker build -f src/main/docker/Dockerfile.jvm -t quarkus/code-with-quarkus-jvm .
#
# Then run the container using:
#
# docker run -i --rm -p 8080:8080 quarkus/code-with-quarkus-jvm
#
# If you want to include the debug port into your docker image
# you will have to expose the debug port (default 5005) like this :  EXPOSE 8080 5050
# 
# Then run the container using : 
#
# docker run -i --rm -p 8080:8080 -p 5005:5005 -e JAVA_ENABLE_DEBUG="true" quarkus/code-with-quarkus-jvm
#
###
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.1

ARG JAVA_PACKAGE=java-11-openjdk-headless
ARG RUN_JAVA_VERSION=1.3.5

ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en'

# Install java and the run-java script
# Also set up permissions for user `1001`
RUN microdnf install curl ca-certificates ${JAVA_PACKAGE} \
    && microdnf update \
    && microdnf clean all \
    && mkdir /deployments \
    && chown 1001 /deployments \
    && chmod "g+rwX" /deployments \
    && chown 1001:root /deployments \
    && curl https://repo1.maven.org/maven2/io/fabric8/run-java-sh/${RUN_JAVA_VERSION}/run-java-sh-${RUN_JAVA_VERSION}-sh.sh -o /deployments/run-java.sh \
    && chown 1001 /deployments/run-java.sh \
    && chmod 540 /deployments/run-java.sh \
    && echo "securerandom.source=file:/dev/urandom" >> /etc/alternatives/jre/lib/security/java.security

# Configure the JAVA_OPTIONS, you can add -XshowSettings:vm to also display the heap size.
ENV JAVA_OPTIONS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager"

COPY target/lib/* /deployments/lib/
COPY target/*-runner.jar /deployments/app.jar

EXPOSE 8080
USER 1001

ENTRYPOINT [ "/deployments/run-java.sh" ]

6. Run and Test Changes


As of now, You feel all are good in place for the Resteasy JAX-RS endpoints and now time to start the application to hit the first request.

And also here there is no main application with main() method similar to the Spring Boot application.

First, open the terminal and run the below command and go the where the pom.xml file is located.

./mvnw compile quarkus:dev:

This command will download all the dependencies first and next then compiles and start the container.

MacBook-Pro-2:code-with-quarkus$ ./mvnw compile quarkus:dev

[INFO] Scanning for projects...

[INFO] 

[INFO------------< org.javaprogramto.quarkus:code-with-quarkus >-------------

[INFOBuilding code-with-quarkus 1.0.0-SNAPSHOT[INFO--------------------------------[ jar ]---------------------------------

[INFO] 

[INFO--- maven-resources-plugin:2.6:resources (default-resources) @ code-with-quarkus ---

[INFO] Using 'UTF-8' encoding to copy filtered resources.

[INFO] Copying 2 resources

[INFO] 

[INFO--- maven-compiler-plugin:3.8.1:compile (default-compile) @ code-with-quarkus ---

[INFO] Changes detected - recompiling the module!

[INFO] Compiling 1 source file to /Users/Documents/Quarkus/code-with-quarkus/target/classes[INFO]

 [INFO--- quarkus-maven-plugin:1.4.1.Final:dev (default-cli) @ code-with-quarkus ---Listening for transport dt_socket at address: 5005


Downloaded from central: 

https://repo.maven.apache.org/maven2/io/quarkus/quarkus-kubernetes-deployment/1.4.1.Final/quarkus-kubernetes-deployment-1.4.1.Final.jar (89 kB at 9.2 kB/s)Downloaded from central: 

https://repo.maven.apache.org/maven2/io/dekorate/kubernetes-annotations/0.11.5/kubernetes-annotations-0.11.5-noapt.jar (321 kB at 33 kB/s)Downloaded from central: 

https://repo.maven.apache.org/maven2/io/fabric8/kubernetes-model/4.9.0/kubernetes-model-4.9.0.jar (12 MB at 1.0 MB/s)Downloaded from central: 

https://repo.maven.apache.org/maven2/io/dekorate/dekorate-dependencies/0.11.5/dekorate-dependencies-0.11.5.jar (19 MB at 1.1 

MB/s)__  ____  __  _____   ___  __ ____  ______  --/ __ \/ / / / _ | / _ \/ //_/ / / / __/  -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \   --\___\_\____/_/ |_/_/|_/_/|_|\____/___/   2020-05-03 20:15:10,225 

INFO  [io.quarkus] (Quarkus Main Thread) code-with-quarkus 1.0.0-
SNAPSHOT (powered by Quarkus 1.4.1.Final) started in 1.729s. Listening on: http://0.0.0.0:80802020-05-03 20:15:10,228 

INFO  [io.quarkus] (Quarkus Main ThreadProfile dev activated. Live Coding activated.2020-05-03 20:15:10,228 

INFO  [io.quarkus] (Quarkus Main ThreadInstalled features: [cdi, kubernetes, resteasy]



Now the services is started on port 8080. You can see from console what are the installed libraries on this [cdi, Kubernetes, resteasy]

Let us test the service with the curl command.

MacBook-Pro-2:code-with-quarkus$ curl localhost:8080/hello
hello world


This has produced the output hello world.


7. Quarkus Hot Reload Feature


Now, the power of Quarkus is to fetch the latest changes when a request come to the server. Hot-reload capability is added by default with the command of "./mvnw compile quarkus:dev:"

In simpler words, All the changes to java and configuration files are compiled when next request comes or the browser is refreshed.

Now, appended the extra text to the response and then just hit the request again.

@Path("/hello")
public class ExampleResource {

    @GET    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "hello world, Welcome reader.";
    }
}

After hitting curl command again you will see the below changes in the console and get latest response.

[2020-05-03 20:25:04,012 INFO  [io.qua.dep.dev] (vert.x-worker-thread-1) Hot replace total time: 0.777s ]


Internally, the vert-x thread will be pulling the latest changes from compiled versions.

[MacBook-Pro-2:code-with-quarkus$ curl localhost:8080/hellohello, world, Welcome reader.]

Similarly, you can bundle the application as jar and run the application with java -jar command..

To create the jar file:

[./mvnw package]

To start the application:

After packaging success, you will see the two jars in the target folder.

[code-with-quarkus-1.0.0-SNAPSHOT.jar
code-with-quarkus-1.0.0-SNAPSHOT-runner.jar]

You should use code-with-quarkus-1.0.0-SNAPSHOT-runner.jar file in java command to start the container.

[java -jar /target/code-with-quarkus-1.0.0-SNAPSHOT-runner.jar]


8. Quarkus Native Image


Usually, native images will help in improving the boot startup time and serve response first.

To create a native image file, you must have GraalVM installed on your machine and must have set GRAALVM_HOME, JAVA_HOME. Otherwise, you will get the below error.

Command to create all the code for the native image

[./mvnw package -Pnative]
Error:
Failed to execute goal io.quarkus:quarkus-maven-plugin:1.4.1.Final:build (default) on project code-with-quarkus: Failed to build quarkus application: io.quarkus.builder.BuildException: Build failure: Build failed due to errors[ERROR] [error]: Build step io.quarkus.deployment.pkg.steps.NativeImageBuildStep#build threw an exception: java.lang.RuntimeException: Cannot find the `native-image` in the GRAALVM_HOME, JAVA_HOME and System PATH. Install it using `gu install native-image`]


If execution is a success then it will take a few seconds to generate artifacts properly.

To verify artifacts

[./mvnw package verify -Pnative]

To create a docker image, you must have a running docker in the machine.

This command generates Linux 64 bit executables.


[./mvnw package -Pnative -Dnative-image.docker-build=true]


Docker Image Creation:


[docker build -f src/main/docker/Dockerfile.native -t Quarkus/code-with-quarkus .]


To run the docker image


[docker run -i --rm -p 8080:8080 Quarkus/code-with-quarkus]



9. Deploying to OpenShift



Once you're done with the testing locally using Docker then you are ready to deploy our container to OpenShift

Assuming that you have the Docker image on our registry and you can deploy the application following the steps below:


oc new-build --binary --name=code-with-quarkus -l app=code-with-quarkus
oc patch bc/quarkus-project -p '{"spec":{"strategy":{"dockerStrategy":{"dockerfilePath":"src/main/docker/Dockerfile.native"}}}}'
oc start-build quarkus-project --from-dir=. --follow
oc new-app --image-stream=quarkus-project:latest
oc expose service quarkus-project


Command to start the application:


[oc get route]


Finally, You'll have to access the same endpoint 


[$ curl http://quarkus-project-myproject.192.168.64.2.nip.io/hello]

Response:

[hello world, Welcome reader]


10. Conclusion

[oc new-build --binary --name=code-with-quarkus -l app=code-with-quarkus

oc patch bc/quarkus-project -p '{"spec":{"strategy":{"dockerStrategy":{"dockerfilePath":"src/main/docker/Dockerfile.native"}}}}'


oc start-build quarkus-project --from-dir=. --follow


oc new-app --image-stream=quarkus-project:latest


oc expose service quarkus-project]


In conclusion, You've seen how to start working with Quarkus Hello World application and how to create the docker image and start the container.

And also steps to deploy the quarkus app into OpenShift


All the code is shown in this article is over GitHub.

You can download the project directly and can run in your local without any errors.



If you have any queries please post in the comment section.

COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Quarkus (Native Image + Docker Deploy OpenShift)
Quarkus (Native Image + Docker Deploy OpenShift)
Learn about QuarkusIO and if it's an alternative for bringing Java more effectively to the cloud. Live hot reloaded changes example, creating native docker and deploy the image to openshift example.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJIJ-7HL5zNKOxiPlkbNJwCuw-A9aFX_j53ED-pifUHrNiRV18wTlCrdjE-CEYhYzuemIIsKL0n5YtHgBKxX44CufA4XGGWZMDP4l2PKr0HqzyyGwHoxPRgCfP2_wCgEEs9yvVTf7710k/s640/Guide+to+QuarkusIO+%2528Native+Image+%252B+Docker+Deploy+OpenShift%2529-min.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJIJ-7HL5zNKOxiPlkbNJwCuw-A9aFX_j53ED-pifUHrNiRV18wTlCrdjE-CEYhYzuemIIsKL0n5YtHgBKxX44CufA4XGGWZMDP4l2PKr0HqzyyGwHoxPRgCfP2_wCgEEs9yvVTf7710k/s72-c/Guide+to+QuarkusIO+%2528Native+Image+%252B+Docker+Deploy+OpenShift%2529-min.png
JavaProgramTo.com
https://www.javaprogramto.com/2020/05/quarkus-native-image-docker-deploy-openshift.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2020/05/quarkus-native-image-docker-deploy-openshift.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content