Click here to Skip to main content
15,907,281 members
Articles / Containers / Docker

Architecture with Docker for Two-tier Application

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
9 Oct 2018CPOL3 min read 12.1K   3   1
This article will share knowledge of creating a two-tier application using Docker Container.

Introduction

Docker is widely used in the IT industry nowadays, due to the ability to share main Operating System resources across all virtual machines (containers in Docker language) created on it. In this article, we will see how we can create a Docker Container and how we can use in Docker Container in a simple two-tier application.

Background

What is Two-Tier Application?

In a common application architecture, only two main Tiers are used.

  1. Web Tier - Where front-end application code is hosted. Front-end application is openly accessible over the internet.
    • Software Stack: Apache and PHP
  2. Database Tier - Where the application Database engine and Database is hosted. The database is only accessible from Web Tier only.
    • Software Stack: MySQL

Note: In N-Tier Architecture, there are three tiers. Please refer to Tiered Architecture Article from CodeProject for more details. In Two-Tier Architecture, Presentation Tier and Business Logic Tier are combined into Web Tier.

Why Docker instead of Virtual Machine?

In the IT Industry, separate Virtual Machine or the Container is used for single Tier. In the traditional Virtualization where on Hypervisor, we create different Virtual Machines for each Tier, whereas in Docker Engine, we create different Containers. Moving the Virtual Machine from one place to another is a tedious task due to many reasons like application dependent libraries, installed software, etc., whereas, in Docker, it is very simple because Docker containers create a bundle of software in a complete file system which contains all application specific and the server related files.

Docker Vs. Virtual Machine

Image Source copyrights belong to nestify.io.

Using the Code

1. Single Server Architecture

Image 2

In this architecture, we need a single server on which we need to create two Docker Containers. One is for Front-End and another one is Database Container.

The following command will create a Docker Container with PHP and Apache. This will cover Web Tier in the server.

PowerShell
//Syntax:
docker run -d -p 80:80 --name apache-php-name -v "$PWD":/var/www/html php:7.2-apache

where apache-php-name is a Docker container name, /var/www/html is the default path to install Apache, 80:80 will point Docker Container's 80 port with Virtual Machines 80 port.

PowerShell
//Commands:
docker pull php:7.2-apache
docker run -d -p 80:80 --name apache-php-web-tier -v "$PWD":/var/www/html php:7.2-apache

Execute the following command on the same server to run a Docker Container with MySQL Database. This will cover Database Tier in the same server.

PowerShell
//Syntax:
docker run --name mysql-name -e MYSQL_ROOT_PASSWORD=mysql-password -d mysql:version-tag

where mysql-name is a MySQL container name, mysql-password is the password to be set for MySQL database root user. version-tag is the tag specifying the MySQL version. By default, MySQL will start on:

BAT
//Commands:
docker pull mysql
docker run --name mysql-db-tier -e MYSQL_ROOT_PASSWORD=Admin@Pass -d mysql

Check the Containers using the following command:

PowerShell
docker ps

Output

Image 3

To remove all containers, use the following command:

PowerShell
docker stop $(docker ps -aq)

Output

Image 4

2. Multiple Server Architecture

Image 5

In this architecture, we need two separate servers. Each server is contained in a separate tier as shown in the above diagram. Virtual Machine #2 is running Database Container and this container is only accessible from the Virtual Machine #1. This is to avoid opening Database server access to the world.

Consider we have two Virtual Machines, VM#1 and VM#2.

On VM#1, run the following commands, to setup PHP and Apache Docker Container.

PowerShell
//Commands: 
docker pull php:7.2-apache
docker run -d -p 80:80 --name apache-php-web-tier -v "$PWD":/var/www/html php:7.2-apache

On VM#2, run the following commands to setup MySQL Docker Container.

PowerShell
//Commands:
docker pull mysql docker run --name mysql-db-tier -e MYSQL_ROOT_PASSWORD=Admin@Pass -d mysql

Points of Interest

Creating a Docker container is easy when you maintain the Infrastructure as a code. When you create a code/script to deploy any architecture, then you can reuse it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect PNAJ Innovations Pvt. Ltd.
India India
Young Technology Enthusiast & IT Engineer from Mumbai; Currently holds certifications from Amazon, Microsoft, and Google.

Working as a Cloud Solution Architect with deep expertise in designing, developing and architecting cloud solutions for Public Clouds (Azure & AWS), Private clouds & Hybrid Clouds. Complete exposure towards all the services offered by Amazon Web Services including PaaS, IaaS, Storage, Compute, Virtual Networking, etc.

Cloud Blog - http://pnajinnovations.com/category/posts/blogs/cloud-blog/

Comments and Discussions

 
QuestionContainer to container traffic not explained Pin
Gunnar S4-Mar-19 19:19
Gunnar S4-Mar-19 19:19 
Hello.
Please describe how the traffic between containers is carried out. Quite many details has been left out from the article, right?
Thanks in advance, Gunnar

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.