Payara Platform on Microsoft Azure: Container Instances
Originally published on 02 Oct 2018
Last updated on 02 Oct 2018
Azure Container Instances allow you to rapidly deploy containers to the Microsoft Azure cloud without having to manage virtual machines and the corresponding infrastructure. Container Instances can be used to rapidly deploy Java EE and MicroProfile applications using Payara Micro as the underlying platform for your Cloud Native applications.
In this blog we will walk through deploying a MicroProfile RESTful webservice as a container. Payara Micro supports a full range of APIs and applications ranging from RESTful web services to full web applications that can be deployed on Container Instances.
Preparation
Before we start we will create a resource group to contain the services we will create for this blog. Throughout the process, you will need to create names for resources that are different to the ones used in our examples.
az group create --name payara-aci-rg --location westeurope
Next, we will create a container registry within the same resource group. A container registry is needed to store our Docker images. Alternatively you can publish your image to docker hub or any other registry.
az acr create --name PayaraACIRegistry --resource-group payara-aci-rg --sku Basic --location westeurope --admin-enabled true
To use the registry from Docker we will need to login to the registry. To do so we need to know the credentials which can be found by running
az acr credential show --name PayaraACIRegistry
this will show the username and pasword required for login via docker. note the URL for your container registry which will be shown when creating your registry.
docker login https://payaraaciregistry.azurecr.io
Creating the Base Docker Image
Instead of using the published Payara Micro Docker file for creating our application Docker image, we shall create our own. To do this I will assume you have a web application you wish to deploy and that you have downloaded Payara Micro from the Payara website.
To make things simpler for future applications, first we will create a base image for Payara Micro on Azure and then layer our application on top of the base image. By creating the base image in this manner, you can then reuse it for other applications and microservices while reducing the image sizes we push applications. We will create the base image from the Azul Zulu Open JDK image, as Payara support customers also get support for Azul Zulu Open JDK builds.
Our Dockerfile looks like this:
FROM azul/zulu-openjdk-alpine RUN mkdir -p /opt/payara/deployments RUN wget --quiet -O /opt/payara/payara-micro.jar
https://search.maven.org/remotecontent?filepath=fish/payara/extras/payara-micro/5.182/payara-micro-5.182.jar EXPOSE 80 ENTRYPOINT ["java", "-jar", "/opt/payara/payara-micro.jar"]CMD ["--deploymentDir", "/opt/payara/deployments", "--nocluster", "--port", "80"]
and can be built using:
docker build . -t payara-app-service/base
Creating the Application Image
All we need to do now is layer the war file we wish to deploy onto this image.
I’ve created a simple war file demonstrating a simple MicroProfile JAX-RS web service in this repo, once you’ve cloned the repository build the application using maven and then build a local Docker image using the following command:
docker build . -t payara/azure-micro-example:1.0
You can test this image using this command:
docker run -p8080:80 payara/azure-micro-example:1.0
Once running you can test the REST service using the following URL: http://127.0.0.1:8080/AzureWebAppsExample/webresources/example
Pushing the Docker Image to the Azure Container Registry
Before we can use the created docker image with Azure App Services we need to upload the container to your Azure container registry. We can do this simply with Docker by tagging the local image as a remote image and then using Docker push:
docker tag payara/azure-micro-example:1.0
payaraaciregistry.azurecr.io/azure-micro-example:latest
docker push payaraaciregistry.azurecr.io/azure-micro-example:latest
You can test that your image has been uploaded successfully by using the Azure CLI:
az acr repository list --name payaraaciregistr
Creating the ACI Container
Once our Docker image is available in the container registry we can create a new container instance:
az container create --resource-group payara-aci-rg --name payara-aci-example --image payaraaciregistry.azurecr.io/azure-micro-example:latest --cpu 1 --memory 1 --registry-login-server payaraaciregistry.azurecr.io --registry-username PayaraACIRegistry --registry-password <registry-password> --dns-name-label payara-aci-demo --ports 80
Then, once the instance is created the fully qualified domain name of the instance should be shown in the output of the command, for example:
"ipAddress": {
"dnsNameLabel": "payara-aci-demo",
"fqdn": "payara-aci-demo.westeurope.azurecontainer.io",
"ip": "104.46.38.33",
"ports": [
{
"port": 80,
"protocol": "TCP"
}
]
},
You should be able to access your application running on the domain name shown:
http://payara-aci-demo.westeurope.azurecontainer.io/AzureWebAppsExample/webresources/example
You can get the logs from your container:
az container logs --resource-group payara-aci-rg --name payara-aci-example
Pass Configuration to your Application
If you hit the REST endpoint now you will see that the environment variable injected via the Config API is not configured. Environment variables can be set on creation using the --environment-variables flag. For example, to create the container passing in environment variables use the full command:
az container create --resource-group payara-aci-rg --name payara-aci-example --image payaraaciregistry.azurecr.io/azure-micro-example:latest --cpu 1 --memory 1 --registry-login-server payaraaciregistry.azurecr.io --registry-username PayaraACIRegistry --registry-password <your-registry-password> --dns-name-label payara-aci-demo --ports 80 --environment-variables exampleproperty=hello-world
If you have followed along with this blog you will have deployed a MicroProfile RESTful webservice as a container to the Microsoft Azure cloud using Payara Micro. Learn more about Payara Micro, download the lightweight middleware platform, get Payara Micro Docker images, or watch demos on the website: https://www.payara.fish/payara_micro.
Related Posts
The Payara Monthly Catch - November 2024
Published on 28 Nov 2024
by Chiara Civardi
0 Comments
The Payara Monthly Catch - October 2024
Published on 30 Oct 2024
by Chiara Civardi
0 Comments