Two-tier Web Application Deployment using Docker Compose

Girdhar KajalGirdhar Kajal
3 min read

Introduction to Docker Compose ๐Ÿšข

Docker Compose is a tool that was developed to help define and share multi-container applications. With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.

Docker Compose Essentials ๐Ÿณ

Docker Compose empowers you with a set of essential features to manage your containerized applications efficiently

YAML Configuration ๐Ÿ“‹

Docker Compose uses a YAML file, usually named docker-compose.yml, to define your application's services, networks, and volumes. This human-readable configuration provides a clear overview of your application's structure.

What is Two-tier Architecture?

Two-tier architecture, also known as two-layer architecture, is a software architecture design in which an application is divided into two distinct layers or tiers:

Presentation Layer (Client Tier):

  • This is the user interface or the front-end layer of the application that interacts directly with users.

  • It is responsible for presenting data and user interfaces to the users, typically through a graphical user interface (GUI).

  • User interactions, such as input validation and user interface logic, are managed in this layer.

  • The presentation layer communicates with the data layer to retrieve or store data.

Data/Storage Layer (Server Tier):

  • This is the back-end layer that handles data storage, retrieval, and manipulation.

  • It is responsible for managing data storage, databases, and data-related operations.

  • Business logic and application functionality that involve data processing are typically handled in this layer.

  • The data layer communicates with the presentation layer to provide the requested data or to store data submitted by users.

Step 1: Set Up Your Environment ๐Ÿ—๏ธ

  1. Launch an EC2 instance on AWS and SSH into it. If you need help with launching

  2. create a new directory.

     mkdir two-tier-app
      cd two-tier-app
    

    Step 2: Clone the Code ๐Ÿ“‚

    1. Clone the code from GitHub and navigate into the cloned directory:

       https://github.com/kajalDevOps/two-tier-flask-app.git
      

Step 3: Install Docker Compose

sudo apt install docker-compose -y

Step 4: Deploy the Application ๐Ÿš€

Start the application using Docker Compose in detached mode:

docker-compose up

Step 5: Verify Containers

docker ps

Step 6: Explore the Docker Compose File ๐Ÿ“‹

Let's take a closer look at our docker-compose.yaml file:

Step 7: Access MySQL Database

docker exec -it <CONTAINER_ID> bash
mysql -u root -p

Step 8: Create the database and table

use two_tier;

show databases;

select * from messages;

show table;

Step 9: Access the Application ๐ŸŒ

  1. Open port 5000 in your EC2 instance's Security Group inbound rules.

  2. Access the Two-Tier App from your EC2 instance's public IP address:

    Step 10: Test Data Persistence

    1. Test data persistence by shutting down the containers:

       docker-compose down
      
      1. Restart the application using Docker Compose

         docker-compose up -d
        

Congratulations, you've successfully created and enhanced a Two-Tier Project using Docker Compose.

0
Subscribe to my newsletter

Read articles from Girdhar Kajal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Girdhar Kajal
Girdhar Kajal