Java on Azure Container Apps - Build and deploy polyglot apps in two simple steps
Published Dec 15 2023 04:55 AM 2,069 Views
Microsoft

 

Welcome to the first episode of the Java on Azure Container Apps blog series! In case you missed the introductory episode, you can read it here to get a sense of what it’s about and what you can expect to see in the future.

This blog is based on the Spring Petclinic project with a REST backend and an Angular frontend. The objective is to build and deploy Petclinic on Azure Container Apps and we’ll carry it out in 2 steps:

  1. Build and deploy a Spring REST app to Azure Container Apps directly from the source.
  2. Package the front-end Angular app in a container first and deploy it to Azure Container Apps.


Build and deploy a Spring app to Azure Container Apps

As a developer, you always want to prioritize retaining the inner loop workflow and developer experience during the coding and testing phases. You’ll achieve greater efficiency with a code to cloud process for build consistency and rapid app deployment. Azure Container Apps now natively supports building and deploying Java apps directly from source code or Jar files. This is achieved with Paketo Buildpacks, which implement cloud-native buildpack specifications. To build your app, it will go through many phases in the lifecycle, but most notably, the detect and build phase, where dependencies are determined, downloaded, and built into the final image along with your compiled apps. The image will be layered. For example, you may have layers consisting of a base OS, runtime environment, APMs, and finally your application.

Your Application

APM: Dynatrace

Dependencies: Tomcat, JDK

Base OS: Debian 12

 

Other than the obvious convenience factor of not having to build containers, buildpacks offer the following benefits to developers and IT operators:

  • Providing development teams within the organization the freedom to pick their own base OS images and write their own docker files. As a result, however, images are hard to update and it’s uniformly challenging to keep images free of vulnerabilities. Reinforcing software governance and keeping an audit trail are also problematic. The bottom line is that Docker files are very powerful but can be too low level for application developers to use.
  • Cloud-native buildpacks make it easy to go from source code to cloud, as they drive consistency and separate developer concerns from operational maintenance and security.

Let us now deploy our Spring REST app with Cloud-native buildpacks to Azure Container Apps. First, follow this QuickStart from the official documentation and take care of the steps described in the prerequisites and setup sections. After that, git clone the project from https://github.com/spring-petclinic/spring-petclinic-rest.git, and deploy the app to Azure Container Apps with the followingsimple command:

 

az containerapp up \
    --resource-group <resource-group-name> \
    --name spring-petclinic-rest \
    --location <location> \
    --environment <container-app-environment-name> \
    --source <path-to-source-folder> \
    --target-port 9966 \
    --ingress external

 

This command performs many steps. Let’s take a look at the output and understand each step.

  1. Create a resource group.
  2. Create an environment and Log Analytics workspace.
  3. Set the container app's ingress to external with a target port set to 9966. 
  4. Start the build agent.
  5. Move through various phases in the buildpack to build from source code to container.
  6. Create and deploy the container to Azure Container Apps.

Sean_Li_1-1702451587552.png

Common build errors include compiler errors, invalid path to source code, POM.xml not found in the root path, and authentication issue accessing private Maven repo. You have access to the full build log from the file system and can view the reasons for any failure.

Finally, you can browse to your container app at https://spring-petclinic-rest...azurecontainerapps.io. Save this URL as we’ll need to pass it to the frontend app.

REST_APP_URL=https://spring-petclinic-rest...azurecontainerapps.io

 

Packaging the frontend app

The frontend app communicates with the Spring backend app via REST APIs, and it expects to find the REST API at http://localhost:9966/petclinic/api/. We have slightly modified the Angular app so that it can also read this value from an environment variable. You can clone the modified version here: https://github.com/majguo/spring-petclinic-angular.git

The project already comes with a Docker file, and we’ll use acr build to build the image directly on Azure Container Registry. Before we do that, follow this QuickStart to create a container registry first.

To build and push the image use:

 

az acr build \
    --registry <registry-name> |
    --build-arg CONFIG_ENV=aca \
    --image spring-petclinic-angular:latest .

 

Deploy Frontend Angular App to Azure Container Apps

Putting everything together, we’ll use the “az containerapp create” command to deploy the Angular app image to Azure Container Apps. Since the repo is private in Azure Container Registry, you need to provide the username and password in the command. To get the username and password run

 

az acr credential show --name [registry name]          

az containerapp create \
    --resource-group <resource-group-name> \
    --name angular-frontend \
    --environment <ACA-app-environment-name> \
    --image <ACR-instance-name>/spring-petclinic-angular:latest \
    --target-port 8080 \
    --ingress 'external' \
    --registry-server <ACR-instance-name>.azurecr.io \
    --registry-username <user-name> \
    --registry-password <password> \
    --env-vars REST_APP_URL=${REST_APP_URL}

 

--env-vars will override the default REST_APP_URL with the supplied value. Remember to plug in the location of REST_APP_URL from the previous step.

We’ve now deployed the Petclinic polyglot app to Azure Container Apps. If you disregard the setup and steps to push the Angular app to Azure Container Registry, we’ve achieved the rest in only two steps using AZ containerup and AZ containerapp create

 
 

petclinic.png

In addition to deploying from source code, deploying from a JAR file is another common way to deploy Java apps. Visit our official documentation page to learn more about deploying a Java artifact file to Azure Containers Apps.


If you liked this blog post, be sure to pass it to other people who may be interested and stay tuned for the next episode.

 

Co-Authors
Version history
Last update:
‎Dec 15 2023 04:54 AM
Updated by: