Project 1: Spring Boot Banking App

Anjali KashyapAnjali Kashyap
4 min read

Introduction

Spring Boot simplifies the development of Java-based web applications by offering pre-configured templates, tools, and libraries. This guide outlines the steps for creating a Spring Boot Banking App, including setting up an AWS EC2 instance, installing Docker, and deploying the application.

Reference for code : (158) Code With GoldenCat - YouTube

Pre-requisites

1. AWS Account

2. AWS Ubuntu EC2 instance (t2.medium)

3. Docker Installed

4. Docker Compose Installed

Step 1: Cloning the Code

To clone the code from Git, follow these steps:

1. Copy the repository link from the project's GitHub page.

2. Open your terminal or command prompt.

3. Run the following command:

git clone <repository-link>

4. Verify the files and directories using the ls command.

Step 2: Understanding Dockerfile Instructions

A Dockerfile is a text file containing instructions to build a Docker image. Here are key instructions:

1. ENTRYPOINT: Defines the main command that always runs.

2. CMD: Provides default arguments that can be overridden.

Below is the Dockerfile code for this project: -

# --------- Stage 1: Build Stage ---------

# Use the Maven 3.8.3 with OpenJDK 17 image for building the project

FROM maven:3.8.3-openjdk-17 AS builder

# Set the working directory inside the container to /app

WORKDIR /app

# Copy all files from the host's current directory to the /app directory inside the container

COPY . /app

# Build the project using Maven, skipping tests for faster builds

RUN mvn clean install -DskipTests=true

# --------- Stage 2: Runtime Stage ---------

# Use the lightweight Alpine-based OpenJDK 17 image for running the application

FROM openjdk:17-alpine 

# Set the working directory inside the container to /app

WORKDIR /app

# Copy the built JAR file from the builder stage to the /app/target directory in this stage

COPY --from=builder /app/target/*.jar /app/target/bankapp.jar

# Expose port 8080 to allow communication with the container

EXPOSE 8080

# Specify the command to run the application, using the Java runtime to execute the JAR file

ENTRYPOINT ["java","-jar","/app/target/bankapp.jar"]

Step 3: Building a Docker Image

To create a Docker image, use the docker build command. It takes a Dockerfile and a context as inputs. Run the following command:

docker build -t <image-name> .

Multi-stage build technique is an efficient way to reduce the size of your Docker image. A multi-stage build separates the build process from the runtime environment by defining multiple stages in a single Dockerfile. This approach allows you to include all the tools and dependencies required for building the application in one stage while keeping the final image minimal by only including the runtime and the application artifact.

Initially, we built an image named bankapp with a size of 994MB. Later, by utilizing the multi-stage build process, we created a smaller image named bankappmini

Step 4: Running the Docker Container

To create and start a container from a Docker image, use the docker run command:

docker run -d -p 8080:8080 <image-name>

Below we created both containers using required parameters

Some important notes:-

Create a Bridge Network

To create a Docker bridge network for isolated communication, use the command:

docker network create <network-name>

To view Docker Logs

View the application logs use below command:

docker logs <container-name>

Once everything is set up, the banking application will be accessible at http://<server-ip>:8080.

Congratulations, your Spring Boot Banking App is ready!

How application works:-

  • Registered a user: -

  • Logged in with the registered user:-

  • Deposited some amount & able to see in Transactions: -

Spring Boot simplifies the rapid development of Java web applications by offering pre-configured templates and tools. This guide details the prerequisites for starting a Spring Boot project, including configuring an AWS EC2 instance and installing Docker. It provides instructions for cloning a Git repository and explains Dockerfile directives like ENTRYPOINT and CMD. Additionally, the guide covers essential Docker commands, such as docker build and docker run, used to create and execute containers. It also demonstrates how to view Docker images and logs, ensuring smooth application deployment.

0
Subscribe to my newsletter

Read articles from Anjali Kashyap directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anjali Kashyap
Anjali Kashyap