# Deploying Multitier Application with Docker

This guide provides a comprehensive overview of setting up a **Dockerized Banking Application** using both Docker and Docker Compose. It covers the essential steps for deploying the application, setting up MySQL with persistent storage, and organizing services for seamless containerized deployment. Below is a detailed walkthrough:

---

## **Project Overview**

The **BankApp Project** demonstrates containerizing a Spring Boot application using Docker's multi-stage builds and managing MySQL as a database with Docker Compose.

## **Pre-requisites**

* **AWS Account**: Ensure an active AWS account.
    
* **AWS Ubuntu EC2 Instance**: Deploy a t2.medium instance for better performance.
    
* **Docker Installation**: Install Docker on the EC2 instance. using ‘sudo apt install docker.io‘ and add current user using ‘sudo usermod -aG docker $USER && newgrep docker‘
    
* **Docker Compose Installation**: Install Docker Compose for managing multi-container applications. install using ‘sudo apt install docker-compose-v2‘
    

## **Tools Used**

* **Docker (Multi-Stage Builds)**: For building and deploying the application in different stages.
    
* **Maven**: Compiles and packages the Java application.
    
* **Spring Boot**: Backend framework for the BankApp.
    
* **MySQL**: Relational database for storing application data.
    
* **Docker Compose**: Manages and links multiple services.
    

---

### **Deployment Without Docker Compose**

The initial deployment setup involves managing each container manually without a `docker-compose.yml` file.

1. **Clone the Repository**
    
    * Clone the project repository and navigate to the project code.
        
    * **Command**: `git clone <repository-url> && cd <project-directory>`
        
2. **Multi-Stage Dockerfile Setup**
    
    * Create a multi-stage Dockerfile to separate the build environment from the production environment.
        
    
    ```dockerfile
    # Stage 1 - Build Environment
    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"]
    ```
    
3. **Creating Docker Volume for MySQL**
    
    * Use Docker volumes to store MySQL data persistently.
        
    * **Command**: `docker volume create mysql-bankapp`
        
4. **Creating Docker Network**
    
    * Create a Docker network to enable communication between containers.
        
    * **Command**: `docker network create bankapp`
        
5. **Running MySQL Container**
    
    * Run the MySQL container within the `bankapp` network.
        
    * **Command**:
        
        ```bash
        docker run -d --name mysql \
        -e MYSQL_ROOT_PASSWORD=Test@123 \
        -e MYSQL_DATABASE=BankDB \
        --network=bankapp mysql:latest
        ```
        
6. **Build and Run the BankApp Container**
    
    * Build the Docker image for the BankApp and run it within the network.
        
    * **Commands**:
        
        ```bash
        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
        ```
        
7. **Access the Application**
    
    * Configure EC2 inbound rules to allow traffic on port 8080. Access the BankApp at `<EC2-instance-public-IP>:8080`.
        

### **Deployment with Docker Compose**

Using Docker Compose simplifies the multi-container setup.

1. **Stop and Remove Existing Containers**
    
    * Ensure any previous containers are stopped and removed.
        
    * **Command**: `docker stop <container-ids> && docker rm <container-ids>`
        
2. **Docker Compose YAML Setup**
    
    * Create a `docker-compose.yml` file to define services and configurations for MySQL and the BankApp.
        
    
    ```yaml
    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. **Run Docker Compose**
    
    * **Command**: `docker-compose up -d`
        
    * This command will start all services, with MySQL and the BankApp container linked and configured as specified.
        

### **Testing the Application**

Access the application through the EC2 instance's public IP on port 8080. The BankApp should be fully operational, with MySQL data being persistently stored via Docker volumes.

## [For step by step implementation with images follow this Notion notes here](https://www.notion.so/Docker-bankapp-project-12c7311ab980801a929ad23bf654b64d)

## [For code, refer Github Repo](https://github.com/Amitabh-DevOps/banking-app-project).

## Output images for reference :

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730045152889/153b7e60-185c-4bbf-a0a1-4eb84cc02c7d.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730045157081/d31e422a-fa5d-4ddc-a88d-d9f626ce470f.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730045160614/4f863ef8-afed-43f8-a5a7-341e0dc06589.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730045165345/1569556a-f0e1-41ef-93e4-5f905dd96095.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730045170281/fee09b82-bde8-4482-b35c-c2497122bd6f.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730045174979/8aacfdc1-8b55-4223-ae38-41555020cbd7.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730045178884/72a9674e-38ad-4707-a7d7-b52634147c51.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730045181987/3a7f97b3-3cf0-47b6-9465-a4b8534409de.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730045185918/d7cdaa0e-2e49-4a6b-846a-1a8ebab064b2.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730045189189/392a1ecb-a85a-40c6-bf97-5587024452f1.png align="center")

---

This setup efficiently organizes a multi-container banking application, separating concerns with a Docker multi-stage build and Docker Compose for simplified management. The application is scalable and can be deployed on AWS EC2 for real-world testing.
