Search Tutorials


Deploy a Containerized Spring Boot App to Azure App Service | JavaInUse

Deploy a Containerized Spring Boot App to Azure App Service

In this tutorial we will be creating a Spring Boot 3 application and deploying it as an Azure App Service. In the next tutorial we will be creating an Azure API Management service to securely access the deployed Azure App Service.
Azure App Service is a fully managed platform-as-a-service (PaaS) offering from Microsoft Azure for building, deploying, and scaling web apps.

Benefits of Deploying to Azure App Service:

  1. Managed infrastructure: Azure handles server management, patching, and maintenance, allowing you to focus on your application code.
  2. Scalability: Easily scale your application up or down based on demand, either manually or automatically.
  3. High availability: Built-in load balancing and traffic manager for improved reliability.
  4. Continuous deployment: Streamlined integration with Azure DevOps, GitHub, and other CI/CD tools.
  5. Security: Built-in authentication, SSL support, and integration with Azure security services.
  6. Cost-effective: Pay only for the resources you use, with various pricing tiers available.
  7. Multiple language support: Supports various programming languages and frameworks, including Java and Spring Boot.
  8. Easy monitoring: Integrated application insights and diagnostics tools.
  9. Global reach: Deploy your application to data centers worldwide for reduced latency.
  10. Ecosystem integration: Seamless integration with other Azure services for enhanced functionality.
For this we will be using the following workflow.
Deploy a Spring Boot App to Azure App Service

Video

This tutorial is explained in the below Youtube Video.


Implementation

Spring Boot Application

Go to Spring Initializr and create a spring boot application as follows-
Deploy a Spring Boot App to Azure App Spring Initializr
The pom.xml of the created application is as follows-
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.2.7</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.javainuse</groupId>
	<artifactId>boot-azure-apps</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>boot-azure-apps</name>
	<description>Demo project for Spring Boot</description>
	<url/>
	<licenses>
		<license/>
	</licenses>
	<developers>
		<developer/>
	</developers>
	<scm>
		<connection/>
		<developerConnection/>
		<tag/>
		<url/>
	</scm>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>




Next we will be creating a controller class. In this class we will be exposing
package com.javainuse.boot_azure_apps;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {


    @GetMapping("/test")
    public String test() {
        return "Hello JavaInUse";
    }
}
Finally we want the deployed spring boot application to run on port 80 so in application.properties specify this.
server.port=80
If we now start the application and go to localhost:80/test we get the output as follows
Spring Boot 3 Web Example

Dockerize the spring boot application

Next for our spring boot application we create docker file. We have previously implemented Spring Boot + Docker Tutorial Series.
This Dockerfile creates a container for a Java application using a two-stage build process:

Stage 1 (Build):

It starts with a Maven and OpenJDK 17 image to build the app. It copies your project files into the container, then uses Maven to compile and package your Java application into a JAR file.

Stage 2 (Run):

This stage uses a slimmer OpenJDK 17 image. It copies only the JAR file from the build stage, ignoring all the build tools and source code. This results in a smaller final image. The container is set up to expose port 80 and run your Java application when started.

# Stage 1: Build the application
FROM maven:3.8.3-openjdk-17-slim AS build
WORKDIR /home/app
COPY . /home/app
RUN mvn -f /home/app/pom.xml clean package

# Stage 2: Run the application
FROM openjdk:17-jdk-slim
COPY --from=build /home/app/target/*.jar /usr/local/lib/app.jar
EXPOSE 80
ENTRYPOINT ["java","-jar","/usr/local/lib/app.jar"]
Build the docker image as follows
Spring Boot 3 Web Docker Example
docker build -t javainuseapp .

Push the docker image to Azure Container Registry

Go to azure portal and create an azure container registry as follows-
Spring Boot 3 Web Docker ACR push
Once registry is created we will need to tag the built image with the azure registry name as follows-
Spring Boot 3 Web Docker tag image
docker tag javainuseapp javainuseregistry.azurecr.io/javainuseapp
Before the pushing the image to azure container registry, we will need to first login to the azure portal and the azure container registry as follows-
Spring Boot 3 Web Docker azure login
az login

Spring Boot 3 Web Docker ACR login
az acr login -n javainuseregistry
Finally push the image to docker registry as follows-
Spring Boot 3 Web Docker ACR push
docker image push javainuseregistry.azurecr.io/javainuseapp

Create Azure App using image from azure container registry

Go to azure portal->Azure App Services-> Create Web App
Spring Boot 3 Create Azure App
Create a web app as follows. Select publish as container.
Spring Boot 3 Create a web app
Go to Container tab-> select image source as Azure Container Registry. Select the Azure Registry. Here it gives an error that the ACR needs admin rights.
Spring Boot 3 select image source as Azure Container Registry
For the above exception we need to give Javainuseregistry admin rights as follows-
Spring Boot 3 Web Docker ACR push
  az acr update --name javainuseregistry --admin-enabled true
 
If we now go to the container tab and select javainuseregistry and the image we get no error message.
Spring Boot 3 Web Docker ACR push
Review and create the webapp
Spring Boot 3 Web Docker ACR push
Once the deployment of the web app is complete go to the resource
Spring Boot 3 Web Docker ACR push
The spring boot application is successfully deployed as azure web app
Spring Boot 3 Web Docker ACR push
If we now go to javainuseapp.azurewebsites.net/test we can see the output as follows-
Spring Boot 3 Web Docker ACR push

Download Source Code

Download it -
Spring Boot 3 + Azure App Service Example