Deployment of 2-tier application on Docker

Deployment of 2-tier application on Docker

Overview: Dockerized Banking Application

This guide helps you set up a banking application using Docker and Docker Compose. Docker is a tool that packages an application and its dependencies into a container, ensuring it runs smoothly no matter where it's deployed. In this case, we're using Docker to deploy a Spring Boot banking app and its MySQL database.

Real-Life Example:

Think of Docker as a shipping container. Inside the container, you place everything needed for your app to work (like code, libraries, and configurations). Once it's packed, you can ship it to any destination (server or cloud), and it will run the same way every time.


Step-by-Step Setup for a Dockerized Banking App

Before You Start

You’ll need a few things in place:

  • AWS Account: Amazon Web Services (AWS) is where you can run your application in the cloud.

  • Ubuntu EC2 Instance: A virtual machine in AWS where we’ll set up the application.

  • Docker and Docker Compose: Tools that help us containerize and manage the application.


1. Setting Up the EC2 Instance and Docker

1.1 Launch EC2 Instance

Go to AWS and create a virtual machine (EC2 instance) with Ubuntu. Choose the "t2.medium" instance type for better performance.

1.2 Install Docker

On your EC2 instance, you’ll need to install Docker (the tool that creates containers). Run the following commands:

sudo apt update
sudo apt install docker.io
sudo usermod -aG docker $USER
newgrp docker

1.3 Install Docker Compose

Docker Compose helps us manage multiple containers at once. Install it by running:

sudo apt install docker-compose-v2

2. Dockerizing the BankApp

2.1 Clone the Repository

You need to get the app's code onto your EC2 instance. You can do this by running:

Here is my Github repo link: https://github.com/irahulgupta/Springboot-bankapp-docker

git clone <repository-url> && cd <project-directory>

2.2 Create a Dockerfile

A Dockerfile is like a blueprint that defines how to create the container for your app. It’s divided into two stages:

  • Stage 1 (Build): This installs the necessary tools to build the application.

  • Stage 2 (Production): This takes the finished application and prepares it to run in production.

Here’s what the Dockerfile looks like:

# Stage 1
#----------------------------------

# Import docker image with maven installed
FROM maven:3.8.3-openjdk-17 as builder 

# Set working directory
WORKDIR /app

# Copy source code from local to container
COPY . /app

# Build application and skip test cases
RUN mvn clean install -DskipTests=true

#--------------------------------------
# Stage 2
#--------------------------------------

# Import small size java image
FROM openjdk:17-alpine as deployer

# Copy build from stage 1 (builder)
COPY --from=builder /app/target/*.jar /app/target/bankapp.jar

# Expose application port 
EXPOSE 8080

# Start the application
ENTRYPOINT ["java", "-jar", "/app/target/bankapp.jar"]

2.3 Create a Docker Volume for MySQL

To ensure MySQL's data is saved even if the container stops, create a volume:

docker volume create mysql-bankapp

2.4 Create a Docker Network

Create a virtual network where all the containers can communicate with each other:

docker network create bankapp

2.5 Run MySQL Container

Now, let’s run the MySQL database inside a container. This will hold our bank data:

docker run -d --name mysql \
  -e MYSQL_ROOT_PASSWORD=Test@123 \
  -e MYSQL_DATABASE=BankDB \
  --network=bankapp mysql:latest

2.6 Build and Run the BankApp Container

Finally, we build the BankApp container and link it to MySQL:

docker build -t bankapp-mini .
docker run -d --name bankapp-mini \
  -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

2.7 Access the Application

To view the app, go to your EC2 instance’s public IP on port 8080, like this:

http://<EC2-instance-public-IP>:8080

3. Deployment with Docker Compose

Docker Compose simplifies the process by managing multiple containers with a single file.

3.1 Stop and Remove Existing Containers

Before starting fresh with Docker Compose, stop and remove any old containers:

docker stop <container-ids> && docker rm <container-ids>

3.2 Create the Docker Compose File

Create a docker-compose.yml file that defines the services (MySQL and BankApp), how they interact, and where the data is stored.

Here’s a sample docker-compose.yml file:

version: "3.8"

services:
  mysql:
    image: mysql:latest
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: Test@123
      MYSQL_DATABASE: BankDB
    volumes:
      - mysql-bankapp:/var/lib/mysql
    networks:
      - bankapp
    restart: always
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-pTest@123"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 30s

  bankapp:
    image: bankapp-mini
    container_name: bankapp
    environment:
      SPRING_DATASOURCE_USERNAME: "root"
      SPRING_DATASOURCE_PASSWORD: "Test@123"
      SPRING_DATASOURCE_URL: "jdbc:mysql://mysql:3306/BankDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"
    ports:
      - "8080:8080"
    depends_on:
      mysql:
        condition: service_healthy
    networks:
      - bankapp
    restart: always

volumes:
  mysql-bankapp:

networks:
  bankapp:

3.3 Run Docker Compose

Run the following command to start everything:

docker-compose up -d

4. Testing the Application

Once everything is running, access the BankApp by visiting the public IP of your EC2 instance at port 8080.

The app should be fully operational, and all data should be stored in MySQL, which is backed up using Docker volumes to keep it persistent even if containers are stopped.


Here is the output Images of how’s the application looks like:

Conclusion

Using Docker and Docker Compose, you can easily deploy a banking application that is portable and runs consistently across environments. Whether you're testing locally or deploying in the cloud, Docker makes it simple to manage all the components, such as the app and the database.

This setup ensures that your application and its data are well-organized, and allows for scaling or making changes without downtime.