Loading...

Architecture with Docker for Two-tier Application

View: 15    Dowload: 0   Comment: 0   Post by: admin   Category: Development Tools   Fields: Other

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

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

In this architecture, we need a single server on which we need to create two Docker Container. 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. 

//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.

//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.

//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: 

//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:

docker ps

Output

To remove all containers, use the following command:

docker stop $(docker ps -aq)

Output

2. Multiple Server Architecture

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 following commands, to setup PHP and Apache Docker Container.

//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 following commands to setup MySQL Docker Container.

//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.

Architecture with Docker for Two-tier Application

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

Posted on 11-10-2018 

Comment:

To comment you must be logged in members.

Files with category

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd