Skip to content

How to containerize Java applications running on WildFly and deploy to Azure?

Notifications You must be signed in to change notification settings

fsaleemm/JavaWildFlyContainers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

How to containerize Java applications running on WildFly and deploy to Azure?

This repository will detail steps to containerize Java application running on WildFly application server and running them in Azure App Service and Azure Container Apps.

In this tutorial, WildFly 10.1.0 version is being used. However, any version that is downloadable from WildFly site can be used. If you are changing the version, you may have to change the download URL in the WildFly-Base/Dockerfile.

Pre-requisites

All commands are run in a bash shell for this tutorial.

These steps below assume the following:

  1. Azure Subscription is setup.

  2. Azure Container Registry is created

  3. Docker is setup and verify by running the command below.

    docker --version
  4. Clone this repository by running the commands below.

    git clone https://github.com/fsaleemm/JavaWildFlyContainers.git
    cd JavaWildFlyContainers

Build and Test Base Image Running WildFly Locally

Build the WildFly-Base image

cd WildFly-Base
docker build -t wildfly-base .

Run the container locally with WildFly-Base image

docker run -p 8080:8080 wildfly-base

Launch browser and navigate to http://localhost:8080. You should get the below screen.

WildFly 10 running screen

Stop the container by typing Ctrl+c.

Build and Test Application Image using Base WildFly Image Locally

Build the Petstore Application

The Petstore application source code is in this repository.

To build the application package use maven.

git clone https://github.com/agoncal/agoncal-application-petstore-ee7.git
cd agoncal-application-petstore-ee7
mvn clean install -DskipTests

Copy the "war" file in the agoncal-application-petstore-ee7/target directory to WildFly-App directory.

Alternatively, you can use the applicationPetstore.war already in the WildFly-App directory.

Build the WildFly-App image

cd ../WildFly-App
docker build -t <Your-Azure-Container-Registry-Name>.azurecr.io/wildfly-app .

Run the container locally with WildFly-App image

docker run -p 8080:8080 <Your-Azure-Container-Registry-Name>.azurecr.io/wildfly-app

Launch browser and navigate to http://localhost:8080/applicationPetstore/. You should see the application screen below.

Petstore Application

Stop the container by typing Ctrl+c.

Push Image to Azure Container Registry

Login to your Azure Container Registry

az acr login -n <Your-Azure-Container-Registry-Name>

Publish the docker image to your registry

docker push <Your-Azure-Container-Registry-Name>.azurecr.io/wildfly-app

Create Resource group

az login

RESOURCE_GROUP="<Your-Resource-Group>"
LOCATION="<Your-preferred-location>"

az group create --name $RESOURCE_GROUP --location $LOCATION

Deploy to Azure App Service

Create App, set the configuration setting, enable managed identity, give managed identity access to ACR, and deploy image from ACR.

ASP_NAME="<Your-App-Service-Plan-Name>"
APP_NAME="<Your-App-Name>"
REGISTRY_NAME="<Your-Azure-Container-Registry-Name>"
ACR_RESOURCE_GROUP="<Resource Group for Azure Container Registry>" # Allowing for ACR in different RG

# Create App Service Plan
az appservice plan create --name $ASP_NAME --resource-group $RESOURCE_GROUP --is-linux

# Create App
az webapp create --resource-group $RESOURCE_GROUP --plan $ASP_NAME --name $APP_NAME --deployment-container-image-name <Your-Azure-Container-Registry-Name>.azurecr.io/wildfly-app:latest

# Set port as container exposes port 8080. The App Service request port will always be 443 (https)
az webapp config appsettings set --resource-group $RESOURCE_GROUP --name $APP_NAME --settings WEBSITES_PORT=8080

# Enable Managed Identity on App Service
APP_SERVICE_PRINCIPAL=$(az webapp identity assign --resource-group $RESOURCE_GROUP --name $APP_NAME --query principalId --output tsv)

# Get subscription Id
SUBSCRIPTION_ID=$(az account show --query id --output tsv)

# Assign AcrPull role for App Service on Container Registry
az role assignment create --assignee $APP_SERVICE_PRINCIPAL --scope /subscriptions/$SUBSCRIPTION_ID/resourceGroups/$ACR_RESOURCE_GROUP/providers/Microsoft.ContainerRegistry/registries/$REGISTRY_NAME --role "AcrPull"

# Set App Service to use Managed Identity for ACR
az resource update --ids /subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.Web/sites/$APP_NAME/config/web --set properties.acrUseManagedIdentityCreds=True

Launch browser and navigate to https://<Your-App-Name>.azurewebsites.net/applicationPetstore. You should see the application below:

Petstore Application

Deploy to Azure Container Apps (In Preview)

NOTE: The steps below are based on the preview version of Azure Container Apps. The steps may change for the GA version.

In preview version, there is no ACR integration using Managed Identities, so ACR needs to have Admin user enabled.

  1. Run through these Setup steps.

  2. Run through these Create an environment steps.

  3. Create Container App following below steps:

    RESOURCE_GROUP="<Your-Resource-Group>"
    CONTAINERAPPS_ENVIRONMENT="<Your-Container-Apps-Environment-Name>"
    CONTAINERAPPS_Name="<Your-Container-Apps-Name>"
    CONTAINER_IMAGE_NAME="<Your-Azure-Container-Registry-Name>.azurecr.io/wildfly-app:latest"
    REGISTRY_LOGIN_SERVER="<Your-Azure-Container-Registry-Name>.azurecr.io"
    REGISTRY_USERNAME="<Your-ACR-Username>"
    REGISTRY_PASSWORD="<Your-ACR-Password>"
    
    # Create the Container App
    az containerapp create \
    --name $CONTAINERAPPS_Name \
    --resource-group $RESOURCE_GROUP \
    --environment $CONTAINERAPPS_ENVIRONMENT \
    --image $CONTAINER_IMAGE_NAME \
    --registry-login-server $REGISTRY_LOGIN_SERVER \
    --registry-username $REGISTRY_USERNAME \
    --registry-password $REGISTRY_PASSWORD \
    --target-port 8080 \
    --ingress 'external' \
    --query configuration.ingress.fqdn
    

The output of the create container app command will provide the URL of the application.

Launch browser and navigate to https://<URL-FROM-CREATE-CONTAINER-APP-OUTPUT>/applicationPetstore. You should see the below screen:

Petstore Application

References

Migrate custom software to Azure App Service using a custom container

Container Apps QuickStart: Deploy an existing container image with the Azure CLI

Run a custom container in Azure App Service

Application - Petstore Source Code

WildFly Docker Images

Azure-Samples - app-service-wildfly

WildFly downloads

Disclaimer

This is an example intended for illustration purposes and is not production ready code.

About

How to containerize Java applications running on WildFly and deploy to Azure?

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published