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.

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

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"

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>


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}`);
});

STEP 6: Intialize a node application using the following command.
npm init -y
npm install express path
sudo /usr/local/bin/docker-compose up



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.

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.
Add a Comment