Docker Tutorial: Easy 3-Tier Architecture Setup

In the world of software development and deployment, Docker has emerged as a powerful tool that simplifies the process of managing applications. In this blog, we will explore how to set up a simple 3-tier architecture using Docker. This architecture consists of a database layer, an application layer, and a presentation layer, which together form a robust and scalable application framework.

Prerequisites

Before diving into the setup, let's go over a few prerequisites:

  1. Docker Installation: Ensure that Docker is installed on your system. Docker provides different installation packages for Windows, macOS, and Linux. Follow the official Docker installation guide to get Docker up and running on your machine.

  2. What is Docker Hub?: Docker Hub is a cloud-based repository where Docker users and partners create, test, store, and distribute container images. It allows you to find and use images created by others and share your own images.

  3. What is a Docker Image?: A Docker image is a lightweight, standalone, and executable software package that includes everything needed to run a piece of software, including the code, runtime, libraries, environment variables, and configuration files.

  4. Legacy Container Links: In Docker, linking containers allows them to communicate with each other. Although the --link option is considered a legacy feature (with Docker networks being the preferred method now), it is still useful for simple setups and will be used in this tutorial for simplicity.

Setting up the 3-Tier Architecture

Our 3-tier architecture will consist of:

  1. Database Container: This will host our MySQL database.

  2. Application Container: This will run our WordPress application, which connects to the database.

  3. Presentation Layer: This will be the front-end that users interact with, served by the WordPress application.

Step-by-Step Setup

  1. Database Container

    First, we need to create a container for our MySQL database. Use the following command to run the container:

     docker run -dit --name Database \
     -e MYSQL_DATABASE=MySqlDB \
     -e MYSQL_USER=Hrushikesh \
     -e MYSQL_PASSWORD=12345678 \
     -e MYSQL_ROOT_PASSWORD=Mysql \
     mysql:latest
    

    Let's break down this command:

    • docker run -dit: This tells Docker to run the container in detached mode (-d), interactive mode (-i), and allocate a pseudo-TTY (-t).

    • --name Database: This names the container "Database" for easier reference.

    • -e MYSQL_DATABASE=MySqlDB: This sets the environment variable MYSQL_DATABASE to MySqlDB, which is the name of our database.

    • -e MYSQL_USER=Hrushikesh: This sets the environment variable MYSQL_USER to Hrushikesh, which is our database user.

    • -e MYSQL_PASSWORD=12345678: This sets the environment variable MYSQL_PASSWORD to 12345678, which is the password for our database user.

    • -e MYSQL_ROOT_PASSWORD=Mysql: This sets the environment variable MYSQL_ROOT_PASSWORD to Mysql, which is the root password for MySQL.

    • mysql:latest: This specifies the image to use, in this case, the latest version of the MySQL image from Docker Hub.

  2. WordPress Container

    Next, we will create a container for our WordPress application. Use the following command:

     docker run -dit --name wordpress \
     -p 8080:80 \
     --link Database \
     wordpress:latest
    

    Let's break down this command:

    • docker run -dit: Similar to the previous command, this runs the container in detached, interactive mode with a pseudo-TTY.

    • --name wordpress: This names the container "wordpress".

    • -p 8080:80: This maps port 8080 on the host machine to port 80 in the container. This means you can access the WordPress site via http://localhost:8080.

    • --link Database: This links the WordPress container to the Database container, allowing them to communicate.

    • wordpress:latest: This specifies the image to use, in this case, the latest version of the WordPress image from Docker Hub.

  • Communication Setup: Creates a secure communication channel between linked containers.

  • Hostname Resolution: Updates /etc/hosts in the linked container to resolve the source container's name to its IP.

  • Environment Variables: Sets variables with source container details (IP, port, protocol).

  • Automatic DNS: Allows the linked container to access the source container by name.

  • Dynamic IP Handling: Maintains connectivity even if the source container's IP changes.

  • Secure Channel: Ensures isolated and secure communication between containers.

  • Simplified Networking: Reduces manual network configuration for small setups.

Usefulness Despite IP Changes

One of the significant advantages of using --link is its ability to handle IP changes dynamically:

  • Resilient to Restarts: If the MySQL container (Database) is stopped and then restarted (or even replaced), it might receive a new IP address. However, the WordPress container can still connect to it using the same name Database due to the automatic DNS resolution handled by Docker.

  • Consistent Networking: This ensures that your application remains functional without needing to manually update IP addresses or reconfigure networking settings, making it particularly useful in development and testing environments where containers are frequently started, stopped, and recreated.

Summary

By following the steps above, you have successfully set up a simple 3-tier architecture using Docker. Here's a quick recap of what we've done:

  1. Database Layer: We created a MySQL database container.

  2. Application Layer: We created a WordPress container that connects to the MySQL database.

  3. Presentation Layer: The WordPress container serves the front-end to users, accessible via http://localhost:8080.

This setup is a basic example of how Docker can be used to create a scalable and isolated development environment. With Docker, you can easily replicate this setup across different environments, making it a powerful tool for developers.

1
Subscribe to my newsletter

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

Written by

Hrushikesh Dagwar
Hrushikesh Dagwar

Adaptable individual with a passion for continuous learning and growth, I bring a diverse skill set, leadership and mentoring abilities, and technical and creative expertise to any team or individual.