Run Quarkus inside docker | Dockerizing a Quarkus Application

Share on:

This article will guide you through building Docker image for Quarkus application and to run Quarkus application inside the docker container.

What you are building

You are building docker image for Quarkus. Docker is a lightweight Linux container for running applications in a containerized model. In this article we are focusing on building docker image first. After the successful image build we are going to run Quarkus inside Docker container.

Prerequisites

Recently Docker has started its support for Windows operating system also. If you are using Windows 10 then you can use Docker’s installer.

Get your Quarkus Application

Before we start building docker container you need to get your Quarkus application from GitHub Repo. (If you are new to Quarkus then i would highly recommend to go through my first tutorial on Getting Started With Quarkus | Quarkus Tutorial)

The cloned GitHub repository contains a bootstrap “Hello World"program using quarkus.

Here is “Hello World"restend point for our Quarkus application

 1import javax.ws.rs.GET;
 2import javax.ws.rs.Path;
 3import javax.ws.rs.Produces;
 4import javax.ws.rs.core.MediaType;
 5
 6@Path("/hello")
 7public class HelloJhooq {
 8
 9    @GET
10    @Produces(MediaType.TEXT_PLAIN)
11    public String hello() {
12        return "hello world ! From Jhooq";
13    }
14}

Docker.jvm or Docker file

The GitHub project contains a Docker.jvm file and the same file we are goign to use for building our docker container.

 1####
 2# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode
 3#
 4# Before building the docker image run:
 5#
 6# mvn package
 7#
 8# Then, build the image with:
 9#
10# docker build -f src/main/docker/Dockerfile.jvm -t quarkus/hello-world-jvm .
11#
12# Then run the container using:
13#
14# docker run -i --rm -p 8080:8080 quarkus/hello-world-jvm
15#
16###
17FROM registry.access.redhat.com/ubi8/ubi-minimal:8.1
18
19ARG JAVA_PACKAGE=java-1.8.0-openjdk-headless
20ARG RUN_JAVA_VERSION=1.3.5
21
22ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en'
23
24# Install java and the run-java script
25# Also set up permissions for user `1001`
26RUN microdnf install openssl curl ca-certificates ${JAVA_PACKAGE} \
27    && microdnf update \
28    && microdnf clean all \
29    && mkdir /deployments \
30    && chown 1001 /deployments \
31    && chmod "g+rwX" /deployments \
32    && chown 1001:root /deployments \
33    && 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 \
34    && chown 1001 /deployments/run-java.sh \
35    && chmod 540 /deployments/run-java.sh \
36    && echo "securerandom.source=file:/dev/urandom" >> /etc/alternatives/jre/lib/security/java.security
37
38# Configure the JAVA_OPTIONS, you can add -XshowSettings:vm to also display the heap size.
39ENV JAVA_OPTIONS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager"
40
41COPY build/lib/* /deployments/lib/
42COPY build/*-runner.jar /deployments/app.jar
43
44EXPOSE 8080
45USER 1001
46
47ENTRYPOINT  "/deployments/run-java.sh" ]

Build docker image

To build the docker image use the following command

1docker build -f src/main/docker/Dockerfile.jvm -t quarkus/hello-world-jvm .

Once the docker image is built successfully afterwords you should see a following successful message

1Successfully built dca1588f66c3
2Successfully tagged quarkus/hello-world-jvm:latest

docker run : Run your image

Use following docker run command to run your docker image

1docker run -i --rm -p 8080:8080 quarkus/hello-world-jvm

Once you run the above docker run command then you should see the following logs

1exec java -Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager -XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:MinHeapFreeRatio=20 -XX:MaxHeapFreeRati
2o=40 -XX:+ExitOnOutOfMemoryError -cp . -jar /deployments/app.jar
32020-03-25 19:01:10,691 INFO  io.quarkus] (main) hello-world 1.0.0-SNAPSHOT (running on Quarkus 1.2.1.Final) started in 0.993s. Listening on: http://0.0.0.0:8080
42020-03-25 19:01:10,718 INFO  io.quarkus] (main) Profile prod activated.
52020-03-25 19:01:10,719 INFO  io.quarkus] (main) Installed features: cdi, resteasy]

Now your docker container is built and running successfully.

The keypoint over here is - In the docker file, we have exposed the application on port 8080. So docker container will deploy our application on port 8080

docker ps : Running status of Quarkus Application

To check the docker running status you can use - docker ps -a

1$ docker ps -a
2CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                    NAMES
3f140ab48831f        quarkus/hello-world-jvm   "/deployments/run-ja…"   9 minutes ago       Up 9 minutes        0.0.0.0:8080->8080/tcp   lucid_matsumoto

It will return you the active running docker container, which is in our case - quarkus/hello-world-jvm

docker stop : Stop Quarkus application

You can stop the Quarkus application using - docker stop <conatiner_id>

1$ docker stop f140ab48831f
2f140ab48831f

YouTube - Tutorial

Summary

Well if you reach so far then congratulations you have successfully containerized your Quarkus application using docker. Now you can run build and deploy this container to any cloud platform such as AWS, Redhat Openshift.

For similar content please follow us on YouTube