Running Java Web Applications on Docker with Payara Micro

Payara Micro, a lightweight framework for serving your Java Applications, from your WAR files.

Payara Micro Features:

  • It's Small, less than 70 MB in size
  • Ease of use.
  • Automatic and Elastic Clustering
  • Payara Micro is designed for running Java EE Applications in a modern Containerized / Virtualized Environment.
  • Each Payara Micro process will automatically Cluster with other PM processes, allowing each web session resilience and a Fully Distributed Data Cache using Payara’s JCache support, with the help of Hazelcast.
  • Payara Micro also comes with a Java API so it can be embedded and launched from your own Java applications
  • More infromation, have a look at their website

What we will be doing today:

We will be using Docker and Payara Micro for serving our Java Application, and our Java Application will consist of a Simple Web Application that will print out the hostname / container of the running instance.

Writing our Java Application and building the WAR file:

Using an IDE of your choice, but in this case I was using Netbeans.

  • From Netbeans:
  • File -> New Project -> Maven -> Web Application
  • Remove the index.html
  • From Web Pages create New File -> JSP -> name: index

Once you open the index.jsp file, populate the file with the following sample code:

<%-- 
    Document   : index
    Created on : Aug 20, 2017, 6:44:07 PM
    Author     : ruan
    Credit     : https://stackoverflow.com/a/23953058
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.net.*" %> 
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        
    <center>
        <br>
        <h1>Hello World!</h1>
        <br>
        <h3>Test Page with Docker + Payara Micro</h3>
        <p></p>
        <%
            String hostname, serverAddress;
            hostname = "error";
            try {
                InetAddress inetAddress;
                inetAddress = InetAddress.getLocalHost();
                hostname = inetAddress.getHostName();
            } 
              catch (UnknownHostException e) {
                e.printStackTrace();
            }
         %>
            Serving From ContainerId: <%=hostname %>
    </center>
    </body>
</html>

From the project tree, select your project name, and build.

From you bottom console, you will find the path of your war file. In my case it was hello-1.0-SNAPSHOT.war which I renamed to hello.war

Running your WAR file with Payara Micro Docker Image:

Payara has a Payara Micro Image on Docker Hub

Create the Dockerfile:

FROM payara/micro
COPY hello.war /opt/payara/deployments/hello.war

Build the image:

$ docker build -t payara-hello .

Create the Container:

$ docker run -itd --name hello-app --publish 80:8080 payara-hello

Testing your Java Application running on Docker and Payara Micro:

$ curl http://127.0.0.1/hello/
<!DOCTYPE html>
<html>
..
            Hello World!
   Test Page with Docker + Payara Micro</h3>

   Serving From ContainerId: d24f8cd982fc
..
</html>

Java Application on Payara Micro with Docker Swarm:

You can find this example on a docker image which I have published on Docker Hub: rbekker87/payara-containername/

And more detail about this can be found on my Github Repository github.com/ruanbekker/docker-payara-containername