# Deploying a Multi-Tier Application Using Docker

## **Table of contents**

* [Project Overview](https://amitabhdevops.hashnode.dev/deploying-multitier-application-with-docker#heading-project-overview)
    
* [Pre-requisites](https://amitabhdevops.hashnode.dev/deploying-multitier-application-with-docker#heading-pre-requisites)
    
* [Tools Used](https://amitabhdevops.hashnode.dev/deploying-multitier-application-with-docker#heading-tools-used)
    
    * [Deployment Without Docker Compose](https://amitabhdevops.hashnode.dev/deploying-multitier-application-with-docker#heading-deployment-without-docker-compose)
        
    * [Deployment with Docker Compose](https://amitabhdevops.hashnode.dev/deploying-multitier-application-with-docker#heading-deployment-with-docker-compose)
        
    * [Testing the Application](https://amitabhdevops.hashnode.dev/deploying-multitier-application-with-docker#heading-testing-the-application)
        
* [For step by step implementation with images follow this Notion notes here](https://amitabhdevops.hashnode.dev/deploying-multitier-application-with-docker#heading-for-step-by-step-implementation-with-images-follow-this-notion-notes-herehttpswwwnotionsodocker-bankapp-project-12c7311ab980801a929ad23bf654b64d)
    
* [For code, refer Github Repo.](https://amitabhdevops.hashnode.dev/deploying-multitier-application-with-docker#heading-for-code-refer-github-repohttpsgithubcomamitabh-devopsbanking-app-project)
    
* [Output images for reference :](https://amitabhdevops.hashnode.dev/deploying-multitier-application-with-docker#heading-output-images-for-reference)
    

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:

```plaintext
sudo apt install docker.io -y
```

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

```plaintext
sudo usermod -aG docker $USER && newgrp docker
```

### **4️⃣ Install Docker Compose**

Install **Docker Compose** to manage multi-container applications:

```plaintext
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:

```plaintext
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.

```plaintext
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.

```plaintext
docker volume create mysql-bankapp
```

### **2️⃣ Set Up a Dedicated Docker Network**

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

```plaintext
docker network create bankapp
```

### **3️⃣ Deploy the MySQL Container**

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

* ```plaintext
     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**:
    
    ```plaintext
    docker build -t bankapp-mini .
    ```
    
    ### **2️⃣ Run the BankApp Container**
    
    Deploy the container, link it with MySQL, and expose it on **port 8080**:
    
    ```plaintext
    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:
    
    ```plaintext
    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**
    
    ```plaintext
    docker stop $(docker ps -q) && docker rm $(docker ps -aq)
    ```
    
    ### **2️⃣ Create a** `docker-compose.yml` File
    
    Define MySQL and BankApp services:
    
    ```plaintext
    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**
    
    ```plaintext
    docker-compose up -d
    ```
    
    This automatically **orchestrates** MySQL and BankApp with persistent storage.
    
    ### **4️⃣ Test the Application**
    
    Visit:
    
    ```plaintext
    http://<EC2-Public-IP>:8080
    ```
    
    Your **BankApp** is now fully deployed, linked to MySQL, and running smoothly! ✅
    
    ## **Output images for reference :**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1739858870678/a297c409-fec6-490b-870c-b86bb1f2f4ce.png align="center")
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1739858891059/5cdc3670-afb8-4d08-9f1a-fc0b76b92fc9.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1739858924475/423772e1-16dd-489d-b9ba-d0b8a667ae40.png align="center")

  
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. 🚀
