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.
- Web Tier - Where front-end application code is hosted. Front-end application is openly accessible over the internet.
- Software Stack: Apache and PHP
- Database Tier - Where the application Database engine and Database is hosted. The database is only accessible from Web Tier only.
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.
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 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.
//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 the 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 the 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.