Guide to Multitier Application Deployment with Docker

Guide to Multitier Application Deployment with Docker

Introduction

Deploying Multitier Applications with Docker simplifies managing complex software systems by containerizing each layer, such as the frontend, backend, and database. Docker ensures consistency across environments and streamlines deployment using tools like Docker Compose. In this blog, I’ll demonstrate how to deploy a multitier application efficiently with Docker.

Overview

The BankApp Project showcases the power of containerization by using Docker's multi-stage builds to optimize a Spring Boot application and Docker Compose to efficiently manage a MySQL database. This project highlights how to streamline application deployment while ensuring scalability and simplicity in managing dependencies.

Tools Used

Docker: For building and deploying application

Maven: Compile and package Java application in jar

Spring boot: Backend framework for bank app

MYSQL: Relational database for storing application data

Docker Compose: Manage and link multiple services

Let's get started--->

Step 1: Get the source code of BankApp Project

Fork this repo: https://github.com/gourilande/Springboot-BankApp.git

Step 2: Make the server ready for deployment purpose

Create ec2 instance:

Instances can be t2-medium, 2 CPU, 4 GiB Memory, and ports 80, 22 are open to allow incoming traffic

Step 3: Clone the repository and install Docker

Clone this repository

Install Docker and Docker Compose

$ sudo apt-get update

$ sudo apt-get install docker.io

$ sudo usermod -aG docker $USER

$ sudo apt install docker-compose -y

Step 4: Write a Dockerfile


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

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

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

FROM openjdk:17-alpine
WORKDIR /app
COPY --from=builder /app/target/*.jar /app/target/bankapp.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app/target/bankapp.jar"]

Build the Dockerfile using

docker build -t bankapp .

Your docker image should be successfully build and tagged.

Your docker image should be successfully created.

Step 5: Run the Docker image and add Database

Step 6: Establish connection between containers

here we can see two containers BankApp and MySQL

$ 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

$ docker run -itd --name mysql -e MYSQL_ROOT_PASSWORD=Test@123 -e 
MYSQL_DATABASE=BankDB --network=bankapp mysql

Associate the Docker containers for BankApp and MySQL with a Docker network for seamless communication.

Step 7: Deployment with Docker compose

Docker Compose YAML Setup

Create docker-compose.yml file to define services and configuration for MySQL and the BankApp

version: "3.8"
services:
  mysql:
    image: mysql:latest
    container_name: mysql
    environment:
      - MYSQL_ROOT_PASSWORD=Test@123
      - MYSQL_DATABASE=BankDB
    volumes:
      - ./mysql-data:/var/lib/mysql
    networks:
      - bankapp
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 3
      start_period: 30s

  mainapp:
    build: .
    container_name: Bankapp
    environment:
      - SPRING_DATASOURCE_USERNAME=root
      - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/BankDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
      - SPRING_DATASOURCE_PASSWORD=Test@123
    ports:
      - "8080:8080"
    depends_on:
      mysql:
        condition: service_healthy
    networks:
      - bankapp
    restart: always
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:8080/actuator/health || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s

networks:
  bankapp:

Run Docker Compose

docker-compose up -d

This command will start all services, with MySQL and the BankApp container linked and configured as specified.

Step 8: 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 stored via Docker volumes.

https://65.2.167.234:8080

Output

Done!

I hope you learned something today with me!

Stay tuned for my next blog . I will keep sharing my learnings and knowledge here with you.

Let's learn together! I appreciate any comments or suggestions you may have to improve my blog content.

Thank you,

Gouri Lande.