Deploy and Run Distributed Containerized Online Store Web Application in the EC2 instance of Amazon Web Services
In this post, I will explain the steps to deploy the application in the EC2 instance of Amazon Web Services (AWS).
First, we need to create the EC2 instance on Amazon Web Services.
Login to your AWS account (you can create a free account if you don’t have one).
In the AWS Console, under services, select EC2 and click Launch Instance. In the instance, type select Amazon Linux AMI 2017.09.0 (HVM), SSD Volume Type.
I will not go into the detail of creating the instance. See my post Create a simple Virtual Private Cloud (VPC) in Amazon Web Services (AWS), in which I mentioned detailed steps of creating the EC2 instance.
Once an instance is created, establish SSH connection to the instance. Steps to establish SSH connection is also mentioned in the above post.
If you are running Windows, you can use PuTTY to establish the SSH connection.
Next, install the docker on EC2 instance.
Run the following command:
sudo yum update -y
sudo yum install -y docker
sudo service docker start
Next, add the ec2-user to the docker group so you can execute Docker commands without using sudo. Log out and log back in for the settings to take effect:
sudo usermod -a -G docker ec2-user
exit
Establish SSH connection to the EC2 again.
Run docker info command to verify docker up and running.
docker info
Next, install the docker-compose on EC2 instance.
Run following command:
sudo curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` | sudo tee /usr/local/bin/docker-compose > /dev/null
sudo chmod +x /usr/local/bin/docker-compose
sudo service docker start
Note that as of writing this post, the latest version of docker compose was 1.16.1.
We have setup the docker and docker-compose which is the prerequisite to run the Online Store Web application.
Once inside the SSH connection on the EC2 instance, create the directory?onlinestore to store the initscript.sql and docker-compose.yml files.
Run the following command to create the onlinestore directory and then change to that directory.
mkdir onlinestore
cd onlinestore
Next, run the following command to create the initscript.sql file. This will open up the nano editor.
nano initscript.sql
Copy the following script in the nano editor, save the file and exit the editor.
CREATE?USER?'store'@'%'?IDENTIFIED?BY?'password';
grant?all?on?*.*?to?'store'@'%';
CREATE DATABASE IF NOT EXISTS OnlineStore;
USE OnlineStore;
CREATE TABLE IF NOT EXISTS Customers (
CustomerId int NOT NULL AUTO_INCREMENT,
FirstName VARCHAR(255) NOT NULL,
LastName VARCHAR(255) NOT NULL,
EmailAddress VARCHAR(255) NOT NULL,
NotifyMe TINYINT(1) DEFAULT 0,
PRIMARY KEY (CustomerId)
);
Next, run the following command to create the docker-compose.yml file.?This will open up the nano editor.
nano docker-compose.yml
Copy the following script in the nano editor, save the file and exit the editor.
version:?'3'
services:
??onlinestorewebapp:
????image:?mirfanmcs/onlinestorewebapp
????container_name:?OnlineStoreWebApp
????ports:
??????????-?3000:80
????environment:
??????-?ASPNETCORE_ENVIRONMENT=Container
????depends_on:
??????-?onlinestorewebapi
??onlinestorewebapi:
????image:?mirfanmcs/onlinestorewebapi
????container_name:?OnlineStoreWebApi
????ports:
??????????-?3001:80
????environment:
??????-?ASPNETCORE_ENVIRONMENT=Container
??????-?ONLINE_STORE_DB_USERNAME=store
??????-?ONLINE_STORE_DB_PASSWORD=password
-?ONLINE_STORE_DB_SERVER=OnlineStoreDb
??????-?ONLINE_STORE_MQ_USERNAME=store
??????-?ONLINE_STORE_MQ_PASSWORD=password
-?ONLINE_STORE_MQ_SERVER=OnlineStoreMQ
????depends_on:
??????-?rabbitmq
??????-?mysql
??onlinestoreworker:
????image:?mirfanmcs/onlinestoreworker
????container_name:?OnlineStoreWorker
????environment:
??????-?ASPNETCORE_ENVIRONMENT=Container
??????-?ONLINE_STORE_DB_USERNAME=store
??????-?ONLINE_STORE_DB_PASSWORD=password
-?ONLINE_STORE_DB_SERVER=OnlineStoreDb
??????-?ONLINE_STORE_MQ_USERNAME=store
??????-?ONLINE_STORE_MQ_PASSWORD=password
-?ONLINE_STORE_MQ_SERVER=OnlineStoreMQ
????depends_on:
??????-?rabbitmq
??????-?mysql
??rabbitmq:
????image:?rabbitmq:3-management
????container_name:?OnlineStoreMQ
????hostname:?OnlineStoreMQ
????ports:
????????-?15672:15672
????????-?5671:5671
????????-?5672:5672
????volumes:
????????-?~/rabbitmq:/var/lib/rabbitmq/mnesia?
????environment:
????????-?RABBITMQ_DEFAULT_USER=store
????????-?RABBITMQ_DEFAULT_PASS=password
??
??mysql:
????image:?mysql
????container_name:?OnlineStoreDb
????ports:
????????-?3306:3306
????volumes:
????????-?~/mysql:/var/lib/mysql
????????-?~/onlinestore:/home
????command:?--init-file?/home/initscript.sql
????environment:
????????-?MYSQL_ROOT_PASSWORD=password
Instead of creating the files manually, you can copy the files from your local machine to the EC2 instances. You have few options to do that.
If you are running Linux/Mac OSX, you can use the SCP command. If you are running Windows operating system, you can use?FileZilla.
You can also copy the files via S3. In that case, you would need to create the S3 endpoint in the VPC in which your EC2 instance is running and then provide your EC2 instance access to the S3. See this post to learn how to create the S3 endpoint.
You can also transfer the files via FTP. In that case, you would need to add port 21 for FTP under the Custom TCP Rule in the security group attached EC2 instance.
Notice that I put the plain text username and password in the docker compose file?for the sake of simplicity. This is something you should never do in your production environment. You should use the Docker Secret in order to protect the sensitive information in the docker compose file.
With the Docker Secret, you place the sensitive information e.g. password, keys in the file and place the link to the file in the docker compose file. Also, you suffix the environment variable with the FILE, e.g.??MYSQLROOT_PASSWORD_FILE=/run/secrets/mysql-root
领英推荐
When you run the compose file, Docker will pull the following images (if not already exists in your EC2 instance’s Docker repository), map the volume and ports, set the container name, set the environment variables and finally start the containers as per the dependency set.
During the startup of OnlineStoreDb container, Docker will execute the?initscript.sql?script located in the onlinestore?folder. This script will create the user, grant permission, create?OnlineStore?database (if doesn’t exist), and create?Customers?table (if doesn’t exist).
To run the docker-compose.yml execute the following command:
docker-compose -f docker-compose.yml up
This will start the containers.
To verify that?mysql container is running, open the bash of OnlineStoreDb MySQL container by running following command:
docker exec -it 776 bash
Note that 776 is the first three characters of OnlineStoreDb image id. You can get the image id by running docker ps command.
Once you get the root of bash prompt of OnlineStoreDb container, run the following command, and on password prompt, enter the password you setup in the docker compose file:
mysql -u store -p OnlineStore
Once connected to the database run the following command to see the databases:
show database;
+--------------------+
| Database |
+--------------------+
| information_schema |
| OnlineStore |
| mysql |
| performance_schema |
| sys |
+--------------------+
Run the following command to see the tables in the OnlineStore:
use OnlineStore;
show tables;
+-------------------------+
| Tables_in_onlinestore |
+-------------------------+
| Customers |
+-------------------------+
To access the management portal of RabbitMQ and Online Store Website, you need to add the following ports under the Custom TCP Rule in the security group attached EC2 instance.
Note that Security Group in AWS is Stateful which only requires adding the Inbound rules.
To learn more about security group and how to add rules in the security group, see this post.
To access the management portal of RabbitMQ, visit following URL in your browser.
https://54.252.185.183:15672
Note that 54.252.185.183 is my public IP address of EC2 instance in which my application is running. Replace it with your EC2 instance’s public IP address. You can get the public IP address of your EC2 instance in AWS Console.
You can also access the application through the public DNS name assigned to your EC2 instance. It is not so user friendly unless you assign your own custom domain name. I am using public IP address here.
For RabbitMQ management portal login, enter the username and password you setup in the docker-compose.yml file.
After successful log, you will see the dashboard.
Next, we will test our Online Store web application running on the container in EC2 instance.
Visit the following URL in your browser:
https://54.252.185.183:15672
Again, 54.252.185.183 is my public IP address of EC2 instance in which my?application is running. Replace it with your EC2 instance’s public IP address.
Once you see the home page click the Register link and fill out the registration form and then click Register.
Next, click on the Admin link and should be able to see the customer you just registered.
Notice that we are accessing the website through the public IP address which is in my case 54.252.185.183. You can setup your custom domain in the Route 53 and map that to the public IP address.
This completes our setup of Online Store Web application on the Docker container in the EC2 instance of Amazon Web Services.