Deploying a Multi-Tier Application Using Docker

Table of contents

This guide offers a complete walkthrough for setting up a Dockerized Banking Application using Docker and Docker Compose. It outlines the key steps for deploying the application, configuring MySQL with persistent storage, and structuring services for a smooth and efficient containerized deployment. Here's a detailed step-by-step approach.

Pre-requisites

1️⃣ AWS Account

Ensure you have an active AWS account for deployment.

2️⃣ AWS Ubuntu EC2 Instance

Launch a t2.medium instance on AWS EC2 for optimal performance.

3️⃣ Install Docker

Set up Docker on the EC2 instance using the following command:

sudo apt install docker.io -y

Add the current user to the Docker group to avoid using sudo for every command:

sudo usermod -aG docker $USER && newgrp docker

4️⃣ Install Docker Compose

Install Docker Compose to manage multi-container applications:

sudo apt install docker-compose-v2 -y

Tools Used

  • Docker (Multi-Stage Builds) – Efficiently builds and deploys the application in separate stages.

  • Maven – Compiles and packages the Java application.

  • Spring Boot – Serves as the backend framework for the BankApp.

  • MySQL – A relational database for application data storage.

  • Docker Compose – Simplifies the management and linking of multiple containers.


Deployment Without Docker Compose

Step 1: Clone the Repository

Download the project code and navigate to the project directory:

git clone https://github.com/LondheShubham153/Springboot-BankApp && cd <project-directory>

Step 2: Set Up a Multi-Stage Dockerfile

Create a multi-stage Dockerfile to separate the build environment from the production environment, ensuring optimized image size and efficiency.

FROM maven:3.8.3-openjdk-17 AS builder
    WORKDIR /app
    COPY . /app
    RUN mvn clean install -DskipTests=true

    # Stage 2 - Production Environment
    FROM openjdk:17-jdk-alpine
    COPY --from=builder /app/target/*.jar /app/target/bankapp.jar
    EXPOSE 8080
    ENTRYPOINT ["java", "-jar", "/app/target/bankapp.jar"]

Setting Up Persistent Storage and Networking for MySQL

To ensure data persistence and seamless container communication, follow these steps:

1️⃣ Create a Docker Volume for MySQL Storage

This volume ensures that MySQL data remains intact even if the container stops or restarts.

docker volume create mysql-bankapp

2️⃣ Set Up a Dedicated Docker Network

A custom Docker network facilitates secure and isolated communication between containers.

docker network create bankapp

3️⃣ Deploy the MySQL Container

Run the MySQL container and attach it to the bankapp network.

  •    docker run -d --name mysql \
        -e MYSQL_ROOT_PASSWORD=Test@123 \
        -e MYSQL_DATABASE=BankDB \
        --network=bankapp mysql:latest
    
  • Building and Deploying the BankApp Container 🚀

    To containerize and run the BankApp, follow these structured steps:

    1️⃣ Build the Docker Image

    Compile the Spring Boot application and create a Docker image:

      docker build -t bankapp-mini .
    

    2️⃣ Run the BankApp Container

    Deploy the container, link it with MySQL, and expose it on port 8080:

      docker run -d --name bankapp-mini \
        --network=bankapp \
        -e SPRING_DATASOURCE_USERNAME="root" \
        -e SPRING_DATASOURCE_PASSWORD="Test@123" \
        -e SPRING_DATASOURCE_URL="jdbc:mysql://mysql:3306/BankDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC" \
        -p 8080:8080 bankapp-mini:latest
    

    3️⃣ Access the Application

    Ensure EC2 Security Group allows inbound traffic on port 8080, then access the app at:

      http://<EC2-Public-IP>:8080
    

    🔹 Deploying with Docker Compose (Simplified Multi-Container Setup)

    Using Docker Compose eliminates manual container management.

    1️⃣ Stop and Remove Existing Containers

      docker stop $(docker ps -q) && docker rm $(docker ps -aq)
    

    2️⃣ Create a docker-compose.yml File

    Define MySQL and BankApp services:

      yamlCopyEditversion: "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️⃣ Start the Deployment

      docker-compose up -d
    

    This automatically orchestrates MySQL and BankApp with persistent storage.

    4️⃣ Test the Application

    Visit:

      http://<EC2-Public-IP>:8080
    

    Your BankApp is now fully deployed, linked to MySQL, and running smoothly! ✅

    Output images for reference :

This setup effectively streamlines the deployment of a multi-container banking application, leveraging a Docker multi-stage build for optimized image management and Docker Compose for seamless orchestration. By separating concerns, it enhances maintainability and scalability. Additionally, the application can be effortlessly deployed on AWS EC2, making it ready for real-world testing and production use. 🚀