Building a Banking App? Here’s How I Dockerized Mine in 5 Minutes Flat!

Streamline your Spring Boot banking app deployment with multi-stage Docker builds—quick, efficient, and lightweight!

Building a Banking App? Here’s How I Dockerized Mine in 5 Minutes Flat!

This is a Java-based application utilizing the Spring Boot framework, which will be used to create our Banking App. The application is integrated with a MySQL database.

I have added this project on my: GitHub Repo you can fork this project.

PRE-REQUISITES FOR THIS PROJECT:

  • AWS Account

  • AWS Ubuntu EC2 instance (t2.medium)

  • Install Docker

  • Install docker compose

Let's begin.

I will guide you through this project step by step. Please follow the steps below:

  1. Go to AWS console and create EC2 instance with t2.medium instance type:

  1. Once our EC2 instance is created, click on "Connect," then navigate to the SSH section and connect through the terminal.

  2. Run git clone command:

git clone https://github.com/Chetan-Mohod/Springboot-BankApp.git
  1. You can list the contents of your terminal, and you will see that the Spring Boot app project is available.

  1. Now you can remove the Dockerfile and docker-compose.yml file from the directory. Initially, these files are not provided when the developer gives us the code, so we need to create them from scratch.

This is how the developer provides us with the code:

Read all the instructions from README.md.

As a DevOps Engineer, our first task is to create a Docker image so we can run the application. Before creating the Docker image, we need to determine which base image is suitable for this Spring Boot application.

There are many images available, but our primary task is to build our Spring Boot application, which is done using Maven. Therefore, search for a "Maven base Image docker openjdk" on a search engine: Maven Image.

  1. Now, access the EC2 instance terminal and create a Dockerfile. Add the following code:
# ---------------- STAGE 1 ----------------------

# Pull base image to use Maven for building JAR files
FROM maven:3.8.3-openjdk-17

# Working directory where your code and JAR file will be stored
WORKDIR /app

# Copy all the code from the local host to the container
COPY . /app

# Build the app to generate the JAR file
# Maven will install all libraries as per pom.xml
#"-DskipTests is used to skip test cases" if things are not ready at your end then use.
RUN mvn clean install -DskipTests=true

# Expose the port to map it with the host
EXPOSE 8080

#Execute the JAR file using java command and -jar flag (to specify we're executing a jar file)
#Now after installing mvn we have .jar file, now we need to run this jar file
ENTRYPOINT ["java", "-jar", "/bankapp.jar"]
  1. Since this is a new system, we will proceed by installing Docker and updating the system.
#To update the system
sudo apt-get update

#to install docker
sudo apt-get install docker.io -y

#To give permission to our user for docker grp
sudo usermod -aG docker $USER && newgrp docker

#Check docker containers
docker ps
  1. Now, let's proceed to build our Dockerfile:
docker build -t bankapp .
  1. To view the list of Docker images available on your system, execute the following command:

Here, you can observe that the Maven image size is 785MB, which is quite large. To address this, we will use Multi-Stage Docker builds to reduce the size of this image.

  1. Add this code to your Dockerfile to reduce image size:
# ---------------- STAGE 1 ----------------------

# Pull base image to use Maven for building JAR files
FROM maven:3.8.3-openjdk-17 AS builder

# Working directory where your code and JAR file will be stored
WORKDIR /app

# Copy all the code from the local host to the container
COPY . /app

# Build the app to generate the JAR file
# Maven will install all libraries as per pom.xml
#"-DskipTests is used to skip test cases"
RUN mvn clean install -DskipTests=true

# ---------------- STAGE 2 ----------------------

FROM openjdk:17-alpine

WORKDIR /app

#Stage o/p stores in target directory
COPY --from=builder /app/target/*.jar /app/target/bankapp.jar

# Expose the port to map it with the host
EXPOSE 8080

#Execute the JAR file using java command and -jar flag (to specify we're executing a jar file)
#Now after installing mvn we have .jar file, now we need to run this jar file
ENTRYPOINT ["java", "-jar", "/app/target/bankapp.jar"]

Now you can see that the image size has been reduced.

  1. Before proceeding further, we need to create a network.

    • Why create a network? It allows communication between the two containers (bankapp and mysql).
docker network create -d bridge bankapp
  1. Now, let's create a MySQL container:

    • When setting up a database container, it is essential to consult the developer for the names of the environment variables. This information is available in the application.properties file located in our Git repository. These properties are use to run the application.

    • When you pass env variables in application container then that variables are mapped with application.properties.

docker run -itd --name mysql -e MYSQL_ROOT_PASSWORD=Test@123 -e MYSQL_DATABASE=BankDB --network=bankapp mysql
  1. Now, let's create the Application Container:
docker run -itd --name BankApp -e SPRING_DATASOURCE_USERNAME="root" -e SPRING_DATASOURCE_URL="jdbc:mysql://mysql:3306/BankDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC" -e SPRING_DATASOURCE_PASSWORD="Test@123" --network=bankapp -p 8080:8080 bankapp-mini:latest
  1. Now we can check the logs:
docker logs container_id

  1. Now navigate to your AWS instance, then go to the Security Group settings. Edit the Inbound Rules and add an entry for port 8080, allowing traffic from Anywhere (IPv4).

  1. Now, copy the public IP of your instance and paste it into your browser, followed by port 8080.

  1. You can register new users by adding them to the system:

    • After registering some users, proceed to log in:

  • Now Deposit 50k in User1 account

  • To transfer 10k to User2, follow these steps:

  • Click on Transaction:

This is the process for building an application using Multi-Staging Docker.

In the upcoming blog, I will explain how to build this application using Docker Compose.

Interview Question:

  1. CMD vs ENTRYPOINT?

    • CMD: This instruction allows you to specify default commands that can be overridden when the container starts. It provides flexibility to change the command or arguments at runtime.

    • ENTRYPOINT: This instruction sets a fixed command that cannot be overridden at runtime. It ensures that the specified command is always executed when the container starts.

Happy Learning :)

Chetan Mohod

For more DevOps updates, you can follow me on LinkedIn.