Docker Compose 3-Tier Architecture Setup

Docker Compose 3-Tier Architecture Setup

Welcome to our comprehensive guide on setting up a simple 3-tier architecture using Docker Compose! In this tutorial, we'll walk you through the process of creating a robust environment consisting of a database layer, an application layer, and a presentation layer. We'll utilize Docker Compose to orchestrate the deployment and management of these layers seamlessly.

And, For Setting up this architecture without using Docker Compose, You can checkout Docker Tutorial: Easy 3-Tier Architecture Setup.

Prerequisites

Before diving into the tutorial, let's ensure you have the necessary prerequisites:

  1. An active Internet connection: You'll need a reliable Internet connection to access resources and documentation throughout the tutorial.

  2. Docker installation: Ensure Docker is installed on your machine. Docker simplifies the creation and management of containers, which are essential for our setup. Docker Compose, a tool bundled with Docker, streamlines the management of multi-container applications.

Verifying Docker Installation

To verify if Docker and Docker Compose are installed, execute the following commands in your terminal (for Linux and macOS) or PowerShell (for Windows):

$ docker version
$ docker compose version

If Docker is not installed, refer to the official Docker documentation for installation instructions.

Setting up the 3-Tier Architecture

Our architecture comprises three layers:

  1. Database Container: Hosting the MySQL database.

  2. Application Container: Running the WordPress application, which connects to the database.

  3. Presentation Layer: Serving as the front-end interface for user interaction, powered by the WordPress application.

We'll define and configure these components using a docker-compose.yml file.

Docker Compose Configuration

Let's create a new directory for our project and within it, a file named docker-compose.yml. Populate the file with the following configuration:

version: '3.8'

services:
  wordpress:
    container_name: wordpress
    image: wordpress:latest
    depends_on:
      - database
    environment:
      WORDPRESS_DB_HOST: database
      WORDPRESS_DB_USER: Hrushikesh
      WORDPRESS_DB_PASSWORD: 12345678
      WORDPRESS_DB_NAME: MySqlDB
    ports:
      - "8080:80"
    networks:
      - custom_network

  database:
    container_name: database
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: Mysql
      MYSQL_DATABASE: MySqlDB
      MYSQL_USER: Hrushikesh
      MYSQL_PASSWORD: 12345678
    volumes:
      - /data:/var/lib/mysql
    networks:
      - custom_network

networks:
  custom_network:
    driver: bridge

docker-compose.yml:

  • version: '3.8': Specifies the version of the Docker Compose file format in use.

  • services: Defines the services (containers) comprising our application.

WordPress Service

  • container_name: wordpress: Assigns the name 'wordpress' to the container.

  • image: wordpress:latest: Utilizes the latest WordPress image from Docker Hub.

  • environment: Sets environment variables for the WordPress container, specifying database connection details.

  • ports: Maps port 80 of the container to port 8080 on the host machine for accessing the WordPress application.

  • networks: Associates the service with the custom network custom_network.

  • depends_on: Crucially, this specifies that the WordPress container depends on the database container. The depends_on directive ensures that the database container starts before the WordPress container. This order is essential because WordPress requires a functional database to operate.

    • Essential for interdependent services in multi-container apps.

    • Specifies startup order for services

Database Service

  • container_name: database: Names the container 'database'.

  • image: mysql:latest: Utilizes the latest MySQL image from Docker Hub.

  • environment: Specifies environment variables for configuring the MySQL container, including passwords and database settings.

  • volumes: Mounts a volume to persist database data, ensuring data persistence even if the container is stopped or removed.

  • networks: Connects the service to the custom_network.

Networks

  • custom_network: Defines a custom bridge network facilitating communication between services.

Running the Docker Compose Configuration

To deploy your multi-container application, navigate to the directory containing your docker-compose.yml file and execute:

$ docker compose up -d

This command downloads required images (if not already available) and starts the WordPress and MySQL containers in detached mode.

Verifying the Setup

  1. Open your web browser and navigate to http://localhost:8080. You should encounter the WordPress setup page.

  2. Follow the on-screen instructions to complete the WordPress installation.

Summary

Congratulations! You've successfully set up a simple 3-tier architecture using Docker Compose. By defining and configuring MySQL and WordPress containers within a Docker Compose file, you've established an environment capable of serving web applications seamlessly.

With this foundation, you're well-equipped to explore and expand upon containerized architectures, unlocking the potential for scalable and resilient application deployments.