Project-Two tier-Flask app on docker-compose.

Project-Two tier-Flask app on docker-compose.

Introduction

This is a simple Flask app that interacts with a MySQL database. The app allows users to submit messages, which are then stored in the database and displayed on the frontend. Docker is widely used in the IT industry nowadays, due to the ability to share main Operating System resources across all virtual machines (containers in Docker language) created on it.

What is a Two-Tier Application?

In a common application architecture, only two main Tiers are used.

  1. Web Tier - Where front-end application code is hosted. The front-end application is openly accessible over the internet.

    • Software Stack: Python
  2. Database Tier - Where the application Database engine and Database is hosted. The database is only accessible from the Web Tier only.

    • Software Stack: MySQL

Why Docker instead of Virtual Machine?

In the IT Industry, a separate Virtual Machine or Container is used for a single Tier. In traditional Virtualization on Hypervisor, we create different Virtual Machines for each Tier, whereas in Docker Engine we create different Containers. Moving the Virtual Machine from one place to another is a tedious task due to many reasons like application-dependent libraries, installed software, etc., whereas, in Docker, it is very simple because Docker containers create a bundle of software in a complete file system which contains all application specific and the server related files.

Docker Vs. Virtual Machine

In a Two-Tier Architecture, the Presentation Tier and Business Logic Tier are combined into the Web Tier.

Single Server Architecture

Image 2

In this architecture, we need a single server on which we need to create two Docker containers. One is for Front-End and another-one is Database Container.

A two-tier application running using docker-compose.

Use the below commands to clone the repository on your server.

Commands:

Run docker-compose up command. Both containers should be up and running state.

Commands:

  • docker compose up -d

  • docker ps

Go into MySQL container

  • docker exec -it 1e0b78b2f125 bash (Change container ID of yours)

  • mysql -u admin -p

  • admin

  • use mydb;

  •   CREATE TABLE messages (
          id INT AUTO_INCREMENT PRIMARY KEY,
          message TEXT
      );
    
  • SELECT * FROM messages;

The database is empty.

Run the application using your server IP:5000 (In my case http://0.0.0.0:5000/) as I am running the app on my local Ubuntu server. Enter the data.

Check the data is getting updated in the database.

Commands:

  • docker ps

  • docker exec -it 275a4324ef48 bash

  • mysql -h mysql -u admin -p

  • USE myDb;

  • SELECT * FROM messages;

I hope you enjoy the blog post!

If you find the blog helpful, please do like it and share it with your friends. If you have any doubts or questions related to the topics covered in the blog, feel free to ask them in the comments section. I’ll be more than happy to help!