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

This Month in Azure Static Web Apps | 09/2024

    We are back with another edition of the Azure Static Web Apps Community! :party_popper:   September was yet another month ...

1 year ago

GitHub Copilot for Azure: 6 Must-Try Features

As developers, we are constantly seeking tools that streamline our workflows and boost productivity. … Enter GitHub Copilot for Azure, now in ...

1 year ago

Responsible AI Mitigation Layers

Generative AI is increasingly being used in various kinds of systems to augment humans and infuse intelligent behavior into existing and new a...

1 year ago

Streamline Your Azure Workflow: Introducing GitHub Copilot for Azure in VS Code

I'm excited to announce the public preview of GitHub Copilot for Azure - a new addition to your toolkit that seamlessly integrates with G...

1 year ago

Build Intelligent Apps Code-First with Prompty and Azure AI

      Building Generative AI applications can feel daunting for traditional app developers. What does the end-to-end applicati...

1 year ago

Certificación AI-900 (Fundamentos de IA) con Chicas en IA

La inteligencia artificial ha llegado para quedarse, ¡y más aún con la revolucionaria IA generativa! Para ayudar a los profesionales a especia...

1 year ago

Get certified with Learn Live GitHub series!

GitHub Universe is coming, and Microsoft and GitHub are partnering to offer a new special Learn Live series in Brazilian Portuguese, English a...

1 year ago

Certifícate con Learn Live GitHub en Español

Microsoft y GitHub se han unido para ofrecer una nueva serie especial de Learn Live en inglés y español: GitHub 2024. Del 10 al 24 de Octubre,...

1 year ago

Evaluating generative AI: Best practices for developers

As a developer working with generative AI, you've likely marveled at the impressive outputs your models can produce. But how do you ensure the...

1 year ago

Introducing Azure Product Retirement Livestreams

The Azure Retirements team, in collaboration with key partner groups, is excited t...

1 year ago

Newsletter

Get the latest Dynamics 365 and Power Platform content in your inbox

A curated digest of community blogs, product news, videos, and podcasts — delivered without the noise.

Weekly updates Unsubscribe anytime Fresh community picks
We use your email only for the newsletter and you can unsubscribe at any time.
By subscribing, you agree to the privacy policy.