Deploy Java Spring Boot apps to Azure Container Apps
Članek
This article shows you how to deploy a Java Spring Boot app with sign-in by Microsoft Entra account to Azure Container Apps.
This article assumes that you completed one of the following articles using only the Run locally tab, and you now want to deploy to Azure. These instructions are the same as the ones in the Deploy to Azure tab in these articles:
The Azure Container Apps CLI extension, version 0.3.47 or higher. To install the latest version, use the az extension add --name containerapp --upgrade --allow-preview command.
Use the following Maven command to build the project:
Bash
mvn clean verify
Run the sample project locally by using the following command:
Bash
mvn spring-boot:run
Setup
To sign in to Azure from the CLI, run the following command and follow the prompts to complete the authentication process.
Azure CLI
az login
To ensure you're running the latest version of the CLI, run the upgrade command.
Azure CLI
az upgrade
Next, install or update the Azure Container Apps extension for the CLI.
If you receive errors about missing parameters when you run az containerapp commands in Azure CLI, be sure you have the latest version of the Azure Container Apps extension installed.
Azure CLI
az extension add --name containerapp --upgrade
Opomba
Starting in May 2024, Azure CLI extensions no longer enable preview features by default. To access Container Apps preview features, install the Container Apps extension with --allow-preview true.
Azure CLI
az extension add --name containerapp --upgrade--allow-previewtrue
Now that the current extension or module is installed, register the Microsoft.App and Microsoft.OperationalInsights namespaces.
az group create \
--name$RESOURCE_GROUP \
--location$LOCATION \
Create an environment with an auto-generated Log Analytics workspace.
Azure CLI
az containerapp env create \
--name$ENVIRONMENT \
--resource-group$RESOURCE_GROUP \
--location$LOCATION
Show the default domain of the container app environment. Note down this domain to use in later sections.
Azure CLI
az containerapp env show \
--name$ENVIRONMENT \
--resource-group$RESOURCE_GROUP \
--query properties.defaultDomain
Prepare the app for deployment
When you deploy your application to Azure Container Apps, your redirect URL changes to the redirect URL of your deployed app instance in Azure Container Apps. Use the following steps to change these settings in your application.yml file:
Navigate to your app's src\main\resources\application.yml file and change the value of post-logout-redirect-uri to your deployed app's domain name, as shown in the following example. Be sure to replace <API_NAME> and <default-domain-of-container-app-environment> with your actual values. For example, with the default domain for your Azure Container App environment from the previous step and ms-identity-api for your app name, you would use https://ms-identity-api.<default-domain> for the post-logout-redirect-uri value.
After saving this file, use the following command to rebuild your app:
Bash
mvn clean package
Pomembno
The application.yml file of the application currently holds the value of your client secret in the client-secret parameter. It isn't good practice to keep this value in this file. You might also be taking a risk if you commit the file to a Git repository. For the recommended approach, see Manage secrets in Azure Container Apps.
Update your Microsoft Entra ID app registration
Because the redirect URI changes to your deployed app on Azure Container Apps, you also need to change the redirect URI in your Microsoft Entra ID app registration. Use the following steps to make this change:
Navigate to the Microsoft identity platform for developers App registrations page.
Use the search box to search for your app registration - for example, java-servlet-webapp-authentication.
Open your app registration by selecting its name.
Select Authentication from the menu.
In the Web - Redirect URIs section, select Add URI.
Fill out the URI of your app, appending /login/oauth2/code/ - for example, https://<containerapp-name>.<default domain of container app environment>/login/oauth2/code/.
The default JDK version is 17. If you need to change the JDK version for compatibility with your application, you can use the --build-env-vars BP_JVM_VERSION=<YOUR_JDK_VERSION> argument to adjust the version number.
In this example, the containerapp up command includes the --query properties.configuration.ingress.fqdn argument, which returns the fully qualified domain name (FQDN), also known as the app's URL. Use the following steps to check the app's logs to investigate any deployment issue:
Access the output application URL from the Outputs page of the Deployment section.
From the navigation pane of the Azure Container Apps instance Overview page, select Logs to check the app's logs.
In this module, you learn how to deploy a Spring Boot app to Azure Container Apps. You deploy a Spring Boot application to Azure Container Apps and maintain it using the built-in Java stack.