Deploying a Three-Tier Architecture Using Docker Containers.

Deploying a Three-Tier Architecture Using Docker Containers.

Introduction.

In modern application development, scalability, flexibility, and maintainability are key goals. One effective way to achieve this is by implementing a three-tier architecture, which separates an application into three distinct layers: the presentation layer, the logic layer, and the data layer. Docker containers, with their ability to isolate and package applications, provide an ideal environment for deploying each of these layers independently, while ensuring consistency across various platforms.

In this blog, we will walk you through the process of deploying a three-tier architecture using Docker containers. We will break down the components, explore how Docker can enhance the deployment process, and provide a step-by-step guide to getting your three-tier application up and running. Whether you’re new to Docker or a seasoned developer, this guide will help you leverage containers to create a robust, scalable, and easy-to-manage application architecture.

STEP 1: Create a EC2instance.

Screenshot 2025 02 17 180918

STEP 2: SSH into EC2 Instance.

  • Create a project directory.
  • Change the present directory to project directory.
  • Create a file named docker-compose.yml 
nano docker-compose.yaml
Screenshot 2025 02 17 181130 1

STEP 3: Enter the following script.

version: '3'

services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
    depends_on:
      - app

  app:
    image: node:alpine
    working_dir: /app
    volumes:
      - ./api:/app
    command: sh -c "cd /app && npm install && node app.js"
    depends_on:
      - db

  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_DATABASE: sample_db
      MYSQL_USER: app_user
      MYSQL_PASSWORD: app_password
    ports:
      - "3306:3306"
Screenshot 2025 02 17 181117

STEP 4: Create Application Files.

mkdir api
cd api
nano index.html
Enter the following command.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>  Docker Compose Lab</title>
</head>
<body>
    <h1>Hello from Docker Compose!</h1>
</body>
</html>
Screenshot 2025 02 17 181309 1
Screenshot 2025 02 17 181433 1

STEP 5: create a file named app.js.

nano app.js
Enter the following command.
const express = require('express');
const path = require('path');

const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, '../api/index.html'));
});
app.listen(port, () => {
  console.log(`API listening at http://localhost:${port}`);
});
Screenshot 2025 02 17 181350

STEP 6: Intialize a node application using the following command.

npm init -y
npm install express path
sudo /usr/local/bin/docker-compose up
Screenshot 2025 02 17 181442 1
Screenshot 2025 02 17 181503
Screenshot 2025 02 17 181533

STEP 7: Copy the IPv4 DNS of the EC2 instance and paste it in the address tab. Then, add [: 8080] behind the DNS name and press Enter.

Screenshot 2025 02 17 182138

Conclusion.

Deploying a three-tier architecture using Docker containers not only simplifies the management of your application’s layers but also enhances scalability, security, and portability. By isolating each tier in its own container, you can achieve a highly modular system that is easier to update, maintain, and scale as your application grows.

In this guide, we’ve explored how Docker’s containerization can streamline the process of deploying a robust, production-ready three-tier architecture. By leveraging Docker’s power, you can create an efficient and flexible environment that is ready for both development and deployment, while ensuring consistency across different environments.

With this foundation, you are now equipped to implement your own three-tier architecture with Docker. As you continue to refine your application, Docker will be a key tool in helping you optimize and scale with ease.

Tags: No tags

Add a Comment

Your email address will not be published. Required fields are marked *