Loading...

Azure Tips and Tricks - How to containerize and deploy a Java app to Azure

Azure Tips and Tricks - How to containerize and deploy a Java app to Azure

How to containerize and deploy a Java app to Azure

 


Use containers for platform independence
 
An application that runs in a container behaves the same, wherever you run it. That's because the container provides a stable infrastructure environment that doesn't change. You just take it to wherever you want to run it.

This also enables you to run self-contained applications that do not rely on a runtime, like the Java runtime, to be installed on its host. And this experience is enhanced with cloud-based management and features when you run your containers in a service like Azure Kubernetes Service.

In this post, we'll containerize a Java application, push it to Azure Container Registry, and run it in Azure Kubernetes Service.

Prerequisites
 
If you want to follow along, you'll need the following:
* An Azure subscription (If you don't have an Azure subscription, create a free account before you begin)
* The latest version of Visual Studio Code
* The Git CLI
* And the latest version of the Azure CLI
* An Azure Kubernetes Service cluster. Learn how to create one here
* An Azure Container Registry. Get started here

Containerize a Java app and deploy it to Azure Kubernetes Service
 
In this post, we'll focus on the containerization and deployment of a Java application. Because of that, we start with an existing Azure Kubernetes Service and an Azure Container Registry. Let's get started.

1. Open Visual Studio Code
2. Go to View > Command Palette and type Git: Clone and select the first result
3. Enter the following URL, which is for a sample Java application:
 
https://github.com/Azure-Samples/containerize-and-deploy-Java-app-to-Azure.git

4. Hit Enter and select a location to clone the Git repo
5. When asked, click "Open" to open the Java application
 
160source.png
 
(Sample Java application in VS Code)
 
6. We need to add a Dockerfile to the code to be able to containerize it. Navigate to Project > Airlines in the source code and add a new file with the name "Dockerfile", without a file extension
7. When VS Code detects that you added a Dockerfile, it will prompt you to install the Docker extension. Click Install to install the extension
8. Go back to the newly created Dockerfile and paste in the following code. This specifies a build and package process to containerize the application:
 
# # Build stage # FROM maven:3.6.0-jdk-11-slim AS build WORKDIR /build COPY pom.xml . COPY src ./src COPY web ./web RUN mvn clean package # # Package stage # FROM tomcat:8.5.72-jre11-openjdk-slim COPY tomcat-users.xml /usr/local/tomcat/conf COPY --from=build /build/target/*.war /usr/local/tomcat/webapps/FlightBookingSystemSample.war EXPOSE 8080 CMD ["catalina.sh", "run"]

 

9. Next, we'll build the container. Open a terminal window by right-clicking on the Airlines folder in the file explorer of VS Code, and selecting "Open in integrated terminal"
10. In the terminal, execute the following command to build the container:
 
docker build -t flightbookingsystemsample

 

11. When the build is complete, test the application locally by running the command below:
 
docker run -p 8080:8080 flightbookingsystemsample

 

12. You'll be able to use the application when you open http://localhost:8080/FlightBookingSystemSample in a local browser
 
160result1.png
 
(The containerized Java application running locally)
 
13. Now that we've successfully containerized the application, we will push it to Azure Container Registry and run it in Azure Kubernetes Service. Make sure that you have the names of the resource group that these services are deployed in, and that you have the names of the Azure Container Registry and Azure Kubernetes Service
 
160prerequisites.png
 
(The resource group and service names in Azure)
 
14. We'll feed the resource group and service names into parameters that we can use in later scripts by executing the following command: (replace the values with your own)
 
AZ_RESOURCE_GROUP=kubernetes AZ_CONTAINER_REGISTRY=myjavaregistry AZ_KUBERNETES_CLUSTER=javacluster

 

15. First, make sure that you are logged into Azure by running:
 
az login //optionally execute the following to select the correct Azure subscription az account list --output table az account set --subscription "<YOUR_SUBSCRIPTION_ID>"

 

16. Next, log into the Azure Container Registry:
 
az acr login -n $AZ_CONTAINER_REGISTRY

 

17. Now tag the previously built container image with your Azure Container Registry:
 
docker tag flightbookingsystemsample $AZ_CONTAINER_REGISTRY.azurecr.io/flightbookingsystemsample

 

18. Push the container to Azure Container Registry:
 
docker push $AZ_CONTAINER_REGISTRY.azurecr.io/flightbookingsystemsample

 

160dockerpush.png

 

(Push the container to Azure Container Registry)
 
19. To deploy the container to Azure Kubernetes Service, we'll add a new file to the source code. In the Project > Airlines folder, add a file with the name "deployment.yml"
20. Paste the following code into the file. Replace the name of the Azure Container Registry with your own. These are instructions to take the container from Azure Container Registry and deploy it into Azure Kubernetes Service:
 
apiVersion: apps/v1 kind: Deployment metadata: name: flightbookingsystemsample spec: replicas: 1 selector: matchLabels: app: flightbookingsystemsample template: metadata: labels: app: flightbookingsystemsample spec: containers: - name: flightbookingsystemsample image: <AZ_CONTAINER_REGISTRY>.azurecr.io/flightbookingsystemsample:latest resources: requests: cpu: "1" memory: "1Gi" limits: cpu: "2" memory: "2Gi" ports: - containerPort: 8080 --- apiVersion: v1 kind: Service metadata: name: flightbookingsystemsample spec: type: LoadBalancer ports: - port: 8080 targetPort: 8080 selector: app: flightbookingsystemsample

 

21. To enable the Azure CLI to deploy the container, we need to install the CLI AKS extension with this command:
 
az aks install-cli

 

22. Next, connect to your Kubernetes cluster:
 
az aks get-credentials --resource-group $AZ_RESOURCE_GROUP --name $AZ_KUBERNETES_CLUSTER

 

23. Now apply the deployment file to start the deployment:
 
kubectl apply -f deployment.yml

 

24. Check the deployment with the command below. When it is finished, you'll see that your pod has the status "Running". You can now use the external-ip from service/flightbookingsystemsample to use the application:
 
160result2.png
 
(The containerized Java application running in Azure Kubernetes Service)
 
Conclusion
 
Containers provide a programming-language agnostic platform in which to run applications. And with Azure Container Registry and Azure Kubernetes Service, managing and running your containerized applications is easier and more performant. To learn more, create a trial account today and go and check it out!

Published on:

Learn more
Azure Developer Community Blog articles
Azure Developer Community Blog articles

Azure Developer Community Blog articles

Share post:

Related posts

Sync data from Dynamics 365 Finance & Operations Azure SQL Database (Tier2) to local SQL Server (AxDB)

A new utility to synchronize data from D365FO cloud environments to local AxDB, featuring incremental sync and smart strategies.

1 hour ago

Azure Cosmos DB Conf 2026 — Call for Proposals Is Now Open

Every production system has a story behind it. The scaling limit you didn’t expect. The data model that finally clicked. The tradeoff you had ...

5 hours ago

Powering Real-Time Gaming Experiences with Azure Cosmos DB for NoSQL

Scenario: When Every Millisecond Counts in Gaming Imagine millions of players logging in at the exact moment a new game season launches. Leade...

1 day ago

Access Azure Virtual Desktop and Windows 365 Cloud PC from non-managed devices

Check out this article via web browser: Access Azure Virtual Desktop and Windows 365 Cloud PC from non-managed devices Many organizations use ...

3 days ago

Power Pages + Azure AD B2C: “The Provided Application Is Not Valid” Error

If you are new to configuring Azure AD B2C as Identity Provider in Power Pages, refer Power Pages : Set up Azure AD B2C After completing the s...

6 days ago

Semantic Reranking with Azure SQL, SQL Server 2025 and Cohere Rerank models

Supporting re‑ranking has been one of the most common requests lately. While not always essential, it can be a valuable addition to a solution...

8 days ago

How Azure Cosmos DB Powers ARM’s Federated Future: Scaling for the Next Billion Requests

The Cloud at Hyperscale: ARM’s Mission and Growth Azure Resource Manager (ARM) is the backbone of Azure’s resource provisioning and management...

8 days ago

Automating Business PDFs Using Azure Document Intelligence and Power Automate

In today’s data-driven enterprises, critical business information often arrives in the form of PDFs—bank statements, invoices, policy document...

24 days ago
Stay up to date with latest Microsoft Dynamics 365 and Power Platform news!
* Yes, I agree to the privacy policy