Dockerize Spring Boot Application And Deploy The Docker Image To The Azure Container Instance

Hello everyone, Hope you are doing well. In this tutorial, you will learn how to Dockerize Spring Boot Application and Deploy the Docker image to an  Azure Container Instance.




A little bit of Background

Azure Container Instance

Azure Container Instances is a solution for any scenario that can operate in isolated containers, without orchestration. Run event-driven applications, quickly deploy from your container development pipelines, and run data processing and build jobs.

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 endpointusername 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 the Image to the  Azure Container Instance

Search for "Container instances" like the below image,

Create a container instance,
Select Resource GroupContainer NameImage source, Registry, Image, Image tag etc... Then click on the "Next: Networking" button. 

You will be taken to a page like the below image,
Configure port number as 8080 
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.

Click on the "Overview" and copy the "IP address".

Verify the hello service using Postman,

Popular posts from this blog

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

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

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

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

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