Day 10 - 3 Tier Project using Docker-Compose

 In this practice we will be creating 3-Tier project to containerize the same using docker-compose.

Let's start then,

Prerequisite

=> Docker Must be installed
=> A Good internet connection
=> Docker Compose should be installed
=> Visual Studio Code should be installed 

About Three-Tier project

Three-tier architecture is a software application architecture that organizes applications into three logical and physical computing tiers:
the presentation tier, the application tier and the data tier.

The presentation tier is the user interface layer, where the user interacts with the application.
The application tier is the logic layer, where data is processed using business rules.
The data tier is the database layer, where data is stored and managed.

The main benefit of three-tier architecture is that each tier can be developed, updated and scaled independently, without affecting the other tiers. It also provides better maintainability, flexibility and reusability of code. 


In this practice we will be using below software to prepare the project.

=> Data Base Tier will use MySQL
=> Application Tier will use ubuntu and PHP.
=> Presentation Tier will use Apache.

Complete code for this project can be downloaded from link. three-tier-setup

Details about Docker-Compose file for this project.

version: "3"
services:
  frontend:
    image: httpd:latest
    volumes:
      - "./frontend:/usr/local/apache2/htdocs"
    ports:
      - 3000:80
  backend:
    container_name: simple-backend
    build:
      context: ./
      dockerfile: Dockerfile
    volumes:
      - "./api:/var/www/html/"
    ports:
      - 5000:80    
  database:
    image: mysql:latest
    environment:
      MYSQL_DATABASE: todo_app
      MYSQL_USER: todo_admin
      MYSQL_PASSWORD: password
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
    volumes:
      - "./db:/docker-entrypoint-initdb.d"
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - 8080:80
    environment:
      - PMA_HOST=database
      - PMA_PORT=3306
    depends_on:
      - database
Let we discuss about this docker-compose file as below.All important comment are tagged within "#"

=> Presentation Tier: This tier information is as below.

version: "3" #this denote we are using version 3 docker-compose #
services: #this denote list of services(containers) we are going to use#
  frontend: #this is service(containers) name#
    image: httpd:latest #this is image file which will be pulled from DockerHub#
    volumes: #information about Volume #
      - "./frontend:/usr/local/apache2/htdocs"
    ports: #Port where it will run#
      - 3000:80

=> Application Logic Tier: this tier basically handles all logics from presentation layer.
backend: #this is service(containers) name#
    container_name: simple-backend
    build:
      context: ./
      dockerfile: Dockerfile #to build from Dockerfile which contains some information#
    volumes:
      - "./api:/var/www/html/"
    ports:
      - 5000:80
=> Database Tier: This tier is used to store all information in persistent storage.

database:
    image: mysql:latest
    environment: #List of environment variables used by this container#
      MYSQL_DATABASE: todo_app
      MYSQL_USER: todo_admin
      MYSQL_PASSWORD: password
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
    volumes:
      - "./db:/docker-entrypoint-initdb.d"

=> Utility Container for accessibility: This is specifically designed container for some useful tasks like accessing MySQL DB main page.

phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - 8080:80
    environment:
      - PMA_HOST=database
      - PMA_PORT=3306
    depends_on: #this is specific tag in docker which denotes that this container#
- database # must start post dependend container i.e DB #
     

Directory Structure
Once downloaded attached zip file open the same in VCS tool. And the same will be like below.

Running the Project

Let we run the project as below and see the results.

PS D:\Docker> docker compose up
[+] Building 0.0s (0/0)
[+] Running 4/0
 ✔ Container docker-database-1    Created                                                                     0.0s 
 ✔ Container docker-phpmyadmin-1  Created                                                                     0.0s 
 ✔ Container docker-frontend-1    Created                                                                     0.0s 
 ✔ Container simple-backend       Created                                                                     0.0s 
Attaching to docker-database-1, docker-frontend-1, docker-phpmyadmin-1, simple-backend
docker-database-1    | 2023-07-26 12:03:46+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.34-1.el8 started.
docker-frontend-1    | AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.20.0.4. Set the 'ServerName' directive globally to suppress this message
docker-frontend-1    | AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.20.0.4. Set the 'ServerName' directive globally to suppress this message

Now Open browser and below URL.

http://localhost:3000/


So we can see we are able to fetch data directly from DB, Hence this practice completes now.





No comments:

Post a Comment

Total Pageviews