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:
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:
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.
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
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 .
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.