Dockerize Spring Boot Application and Deploy Docker Image To Azure App Service Using GitHub Actions

Hello everyone, Hope you are doing well. In this tutorial, you will learn how to dockerize the spring boot application and deploy the docker image to the Azure app service using GitHub actions.



A little bit of Background

Azure App Service (Containerization and Docker)

Dockerize your app and host a custom Windows or Linux container in App Service. Run multi-container apps with Docker Compose. Migrate your Docker skills directly to App Service.

Azure Container Registry

Azure Container Registry allows you to build, store, and manage container images and artifacts in a private registry for all types of container deployments. Use Azure container registries with your existing container development and deployment pipelines. Use Azure Container Registry Tasks to build container images in Azure on-demand, or automate builds triggered by source code updates, updates to a container's base image, or timers.


GitHub Actions

GitHub Actions helps you automate your software development workflows from within GitHub. You can deploy workflows in the same place where you store code and collaborate on pull requests and issues. 
In GitHub Actions, a workflow is an automated process that you set up in your GitHub repository. You can build, test, package, release, or deploy any project on GitHub with a workflow. 

Spring Boot

Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can "just run". 



Step 1: Create a container registry using Azure Portal

Sign in to the Azure portal at https://portal.azure.com
Search for "Container registries" like the below image,

Create a container registry, 
Select the "Resource group", enter "Registry name" etc...
Then click on the "Review + create" button.

You will be taken to a page like the below image,
Then click on the "Create" button.

Now, You can see "Deployment is in progress" like the below image. 

Once deployment is completed you can see the "Your deployment is complete" page like the below image.

Next, click on "Access keys"
Copy the "Login server" value, "Username" and "Password" and keep them safe for future purposes.



Step 2: Creating a simple spring boot web application.

First, open the Spring initializr https://start.spring.io/

Then, Provide the Group and Artifact name. We have provided Group name com.knf.dev.demo and Artifact spring-boot-helloworld. Here I selected the Maven project - language Java - Spring Boot 2.7.1 and add Spring web dependency.

Then, click on the Generate button. When we click on the Generate button, it starts packing the project in a .zip(spring-boot-helloworld.zip) file and downloads the project. 
Then, Extract the Zip file.



Step 3: Import the project on your favourite IDE, I am using Visual Studio Code

Final Project Structure



Pom.xml

<?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>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.knf.dev.dem</groupId>
<artifactId>spring-boot-helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-helloworld</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</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>
<finalName>spring-boot-helloworld</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>


Create a Test Controller

@RestController
public class GreetingController {

@GetMapping("/hello")
public String hello() {
return "Hello World! Your application is running";
}
}


Spring Boot Main Driver

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}


Dockerfile

#
# Build stage
#

FROM maven:3.6.3-jdk-11-slim AS build

WORKDIR usr/src/app

COPY . ./

RUN mvn clean package

#
# Package stage
#

FROM openjdk:11-jre-slim

ARG JAR_NAME="spring-boot-helloworld"

WORKDIR /usr/src/app

COPY --from=build /usr/src/app/target/${JAR_NAME}.jar ./app.jar

CMD ["java","-jar", "./app.jar"]


main.yml(Github workflows)

Configure Azure Container Registry endpoint, username and password which we have in our hand. Build a Docker image and push it to Azure Container Registry.  
name: Build a Docker image and Push it to ACR

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: azure/docker-login@v1
with:
login-server: ${{ secrets.ACR_ENDPOINT }}
username: ${{ secrets.ACR_USERNAME }}
password: ${{ secrets.ACR_PASSWORD }}
- run: |
docker build . -t ${{ secrets.ACR_ENDPOINT }}/knowledgefactory:${{ github.sha }}
docker push ${{ secrets.ACR_ENDPOINT }}/knowledgefactory:${{ github.sha }}




Step 4: Create a New Repo and Upload Files on GitHub

First, sign in to Github https://github.com/ 

Then, create a new repository "spring-boot-helloworld".

Then, upload the source code from your local machine to the newly created Github repo.

Next, click on the "Actions" button,
Select the workflow "Build a Docker image and Push it to ACR"

Then, click on the "Run workflow" button,

You will see an image like below, "Build a Docker image and Push it to ACR" process in progress.
After successful execution of the job, you will see an image like the below,



Step 5: Check whether the image is pushed or not in Azure Container Registry

Go to ACR and click on the "Repositories"
Then click on the repository "knowledgefactory".

You will see the image tag id which has been pushed.



Step 6: Deploy container image to Azure App Service

Search for "App Services" like the below image,

Create a Web App,
Select Resource Group, Name, Publish as 'Docker Container', OS etc... Then click on the "Next: Docker" button. 

You will be taken to a page like the below image,
Select "Image Source", "Registry", "Image", and "Tag" like the above image. Then click on the "Review + create" button.

You will be taken to a page like the below image,
Then click on the "Create" button.

Now, You can see "Deployment is in progress" like the below image. 

Once deployment is completed you can see the "Your deployment is complete" page like the below image.



Step 7: Configure the port number

By default, App Service assumes our custom container is listening on either port 80. Our container listens to a port 8080, set the WEBSITES_PORT as 8080 app setting in our App Service app.

Click on the "Configuration" and set the port as 8080.

Click on the "Overview"
 and Restart the service.
Then copy the "URL".


Verify the endpoint using postman,


All the best!

Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java, Spring Boot Mini Project - Library Management System - Download

Java - Blowfish Encryption and decryption Example

Java - DES Encryption and Decryption example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

ReactJS - Bootstrap - Buttons

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Java - How to Count the Number of Occurrences of Substring in a String

Top 5 Java ORM tools - 2024