Bot

Deploying Docker containers with AWS EC2 Instance

 Deploying Docker containers with AWS EC2 Instance


In this article, I will explain you how to deploy your Docker container/Image in AWS EC2 Instance.

What is EC2 instance?

  • An EC2 instance is nothing but a virtual server in Amazon Web services terminology.
  • It stands for Elastic Compute Cloud.
  • It is a web service where an AWS subscriber can request and provision a compute server in AWS cloud.

What is Docker?

  • Docker is a tool designed to make it easier to create, deploy and run applications by using containers, which are formed with the help of Docker Engine.
  • Docker containers are light-weight alternatives to virtual machines and use host OS.
  • With dockers, there is no need to pre-allocate the RAM or disk space, it takes RAM and disk space according to the requirement.

Use of container technology such as Docker has increased drastically within institutions in different sectors.

At first, we will see how to run a server in AWS EC2 instance by manually installing docker and then how to run docker images.

Steps to deploy docker images in EC2 instance:

Step1: Launching EC2 instance

In AWS Console search for EC2 and click on that service, you will redirect to the AWS EC2 page as follows and then Click on Launch Instance.

To run EC2 successfully, select an Amazon Machine Image (AMI). AMI contains all the software configurations (operating system, application server, and applications) required to launch your instance. In this case, we will pick Amazon Linux AMI.

Now, select the Instance Type that determines the type of CPU, storage, network capacity, and memory needed during the installation. Choose t2.micro, and hit the next button as shown below:

Click the next button until you reach to the Configure Security Group page to retain the default settings. A security group is a set of firewall rules that control the traffic for your instance. On this page, you can add rules to allow specific traffic to reach your instance.

For example, if you want to set up a web server and allow Internet traffic to reach your instance, add rules that allow unrestricted access to the HTTP and HTTPS ports.

Enter the Security Group name and then hit the Review and Launch button

After hitting the Launch button you will see a pop-up page that requires you to select a Key Pair. This contains both a public and private key folder that is used to connect to your EC2 Instance securely via SSH. Click on the “create a new key pair” button and give name to the key pair. Go ahead and download your key pair.

Save your key file to a safe place on your system and then click on Launch Instances, it will direct you to the launching page. Hit the “View Instances” button to access the EC2 Instances page.

In this page, you can see the details of EC2 Instances present like public address and launch time. Copy the IP address, because you will use it on SSH to install Docker.

Step2: How to install Docker on EC2 instance.

To install Docker on the EC2 Instance. Go to the location path where your Key Pair is saved, open terminal and type the following command:

ssh -i keypair.pem ec2-user@<EC2-INSTANCE-PUBLIC-IP-ADDRESS>

You have connected to your EC2 instance, Now you can install docker by using the below command:

sudo yum install -y docker

Use this command to start the docker:

sudo service docker start

docker info

If you done everything right, the above command will shows info about Docker installation without showing any errors.

Now you can run your image. In this case, I will show you the sample node app to get something like this:

docker run -d -p 80:8080 chandupriya/nodedemo:0.1

You will note the “-p 80:8080” text which commands Docker on your Container to link port 8080 to port 80 located on the EC2 Instance. Test whether the process has gone through using the following command:

If you get the message “Hello world!” then your Docker container is running in the AWS cloud.

The last step is to visit the public DNS name in your browser, you get the message like this:

Now that you have successfully deployed Docker Container on AWS EC2 everything looks perfect.

Comments