Tutorial: Deploying Java EE Apps on Azure (Part 2)
Tutorial: Deploying Java EE Apps on Azure (Part 2)
Run your Java EE app in a Docker container on Azure Container Instances
Join the DZone community and get the full member experience.Join For Free
This is the second blog in a series that will walk you through the options of running Java EE applications on Azure. The first part provided details on how to deploy a Java EE app to an application server which is set up in a Virtual Machine on Microsoft Azure along with the Azure Database for PostgreSQL service as the backend database.
In this part, we will run the Java EE app as a Docker container on Azure Container Instances. The example used in the blog post is a simple three-tier application that uses Java EE 8 specifications, such as JAX-RS, EJB, CDI, JPA, JSF, Bean Validation. We will use the Payara Server to deploy the application and use PostgreSQL as the relational database.
During the course of the tutorial, we will cover:
- Postgres setup on Azure.
- Dockerize the Java EE app and setup Azure Container Registry to store the Docker image.
- Deploy the application to Azure Container Instances.
- Explore its functionality.
If you don't have a Microsoft Azure account, go ahead and sign up for a free one!. The Azure CLI is a cross-platform command-line experience for managing Azure resources - please intall it using these instructions.
Set your Azure Subscription ID using the Azure CLI which will be used for this tutorial.
To set your Azure subscription ID
Create a resource group that will contain all the services (resources) which you will create as a part of this tutorial. A resource group is like a logical container that holds related resources for an Azure solution. The resource group includes those resources that you want to manage as a group.
To create a resource group
Azure Database for PostgreSQL is a relational database service based on the open-source Postgres database engine. It's a fully managed database-as-a-service offering which is available in two deployment options, as a single server and as a Hyperscale (Citus) cluster
We will be using the single server option for the purposes of this tutorial
We will use the
az postgres server create command to create a Postgres server instance on Azure. First, set up some of the server properties such as the name, admin user etc.
For storage and SKU options, please refer to the documentation
And, then invoke the command to initiate the database instance creation:
The provisioning process will take a few minutes.
To check the details of the Postgres database instance you just provisioned, invoke
az postgres server show command:
You should get a JSON response. Please note down the value for the
fullyQualifiedDomainName attribute as you will be using this to connect to the Postgres instance later.
It should be of the format:
Later in the post, we will see how to deploy the application to Azure Container Instances. But, the Postgres database is not accessible to external services by default. We will use the
az postgres server firewall-rule create command to create a firewall rule to explicitly allow Azure services to access the Postgres instance. This will allow the JavaEE application deployed within Azure Container Instances to communicate with Postgres.
Note: This setting allows network connections from all IPs within the Azure network. For production use, try to configure the most restrictive firewall rules possible
Azure Container Registry is a managed, private Docker registry service to store and manage your private Docker container images (it based on the open-source Docker Registry 2.0). You can use Azure container registries with your existing container development and deployment pipelines, or use Azure Container Registry Tasks to build container images in Azure. Yo can either build on demand, or fully automate builds with triggers such as source code commits and base image updates.
Let's create a registry to store the Docker image for the JavaEE application. We will use the
az acr create command:
We are using the
BasicSKU. Valid value are:
You can login to the registry once it's created and check the login server
You will use the ACR login server name soon. It's value follows the format:
Now that we the have the VM as well as Payara server up and running, we can now deploy our application! Clone the git repository:
You need to enter the Postgres connectivity information to the
<url> attribute of the
<data-source section in
You can find the
The format is as follows:
Here is the list placeholders which form a part of the JDBC URL:
POSTGRES_FQDNwith value of
AZURE_POSTGRES_ADMIN_USERwith admin user name used to provision PG
AZURE_POSTGRES_SERVER_NAMEwith server name used to provision PG
AZURE_POSTGRES_ADMIN_PASSWORDwith admin password used to provision PG
Set the required values
Simply use these commands to replace
Here is an e.g. of what the
<data-source> section will look like:
The application is now configured. Let's build it!
You should have the WAR file available. To confirm
Our application artifact (WAR file) is ready. We can now build the Docker image and push it out to Azure Container Registry. Here is a quick look at the Dockerfile used for this
It builds on top of the base image for
payara/server-full, copies the
WAR file to a folder from where it can be automatically detected and deployed, downloads the Postgres JDBC driver and places it in the appropriate location for the Payara application server. That's it!
To push the image
az acr repository list command to check the image.
Azure Container Instances allows you to run a container in Azure, without having to manage any VMs and or adopt a higher-level service. It is a solution for any scenario that can operate in isolated containers, including simple applications, task automation, and build jobs. For scenarios where you need full container orchestration, including service discovery across multiple containers, automatic scaling, and coordinated application upgrades, please take a look at Azure Kubernetes Service (AKS).
We are now ready to deploy the JavaEE application to Azure Container Instances. When you create a container instance, you can specify a custom DNS name label so your application is reachable at customlabel.azureregion.azurecontainer.io.
We will need the password for Azure Container Registry to run our Docker image in Azure Container Instance.
Enter below command to create the container and enter the password (obtained above) when prompted:
--ports 8080ensures that we can access the application from port
8080. This is because Payara application server serves HTTP traffic on port
The deployment will take sometime. In the meanwhile, you can track the logs using:
You should see log messages indicating successful deployment of the
Use your browser to access
http://[APP_FQDN]:8080/javaee-cafe. You can use the UI to create, delete and see coffees.
APP_FQDN is nothing but the combination of the DNS label and the Azure region e.g.
The application also exposes a REST API for creating, deleting and listing coffees.
Get all coffees
You should see a JSON response listing both the coffee options you just added
Get a coffee by ID
Delete a coffee by ID
cappuccino is now deleted
Once you are done exploring the application, you can delete the resources. Since we used a resource group, its easy executing a single command.
Please be aware that this will delete all the resources in the group which includes the ones you created as part of the tutorial (VM, Postgres etc.) as well as any other service instances you might have if you used an already existing resource group
You learned how to leverage Docker containers to package and deploy the application to Azure Container Instances along with a managed database offering for long term persistence.
Stay tuned for part 3!
Opinions expressed by DZone contributors are their own.