How to install and run Docker Containers on Amazon EC2 Instance?

Reading Time: 5 minutes
How to install and run Docker Containers on Amazon EC2 Instance?

While running Docker on AWS, developers and admins can build, ship, and run highly reliable distributed applications at any scale.  In this blog, we will demonstrate how to install, build and run docker containers on Amazon EC2 instances.

Refer to Part 1 of the blog here: How To Install And Run Docker Container On Amazon EC2 Instance (Part 1)?

What is Docker?

How to install and run Docker Containers on Amazon EC2 Instance?

Docker provides the tools for building, running, testing, and deploying distributed applications that are based on Linux containers. It has the ability to package and run applications in a loosely isolated environment called containers. You can run many containers simultaneously on a given host. They are lightweight and contain everything needed to run the applications, so no need to depend on what is currently being installed on the host. While running Docker on AWS, developers, and admins can build, ship, and run highly reliable distributed applications at any scale.  

What is Amazon EC2?

How to install and run Docker Containers on Amazon EC2 Instance?

Amazon Elastic Computing Cloud (Amazon EC2) is a cloud computing service that offers safe, scalable computing power. It’s intended to make web-scale cloud computing more accessible to programmers. The easy web service interface of Amazon EC2 allows you to quickly obtain and configure capacity. It gives you complete control over your computing resources and allows you to run on Amazon’s tried-and-true computing infrastructure.

Scenario: In this blog, we will demonstrate to launch an EC2 instance, we will create a Dockerfile to run the Ubuntu image, install packages for the Apache web server, and display “Hello World!” content to the web server’s index root.

Hands-on

Steps to be followed:

  • Create and launch the EC2 instance
  • Install Docker on an Amazon EC2 instance
  • Create, build Dockerfile, and run the Docker Container

Create and launch the EC2 instance

Navigate to the EC2 dashboard, and click on Launch Instances.


Choose AMI image 

Choose instance type and configure storage, VPC details

How to install and run Docker Containers on Amazon EC2 Instance?

Give appropriate tags to the instance

How to install and run Docker Containers on Amazon EC2 Instance?

Configure security group for the instance

Review and launch instance 

How to install and run Docker Containers on Amazon EC2 Instance?

Connect to your instance

How to install and run Docker Containers on Amazon EC2 Instance?

Install Docker on an Amazon EC2 instance

Update the installed packages and package cache on your instance.

sudo yum update -y

Install the most recent Docker Engine package.

Start the Docker service.

Add the ec2-user to the docker group so you can execute Docker commands without using sudo

Log out and log back in again to pick up the new docker group permissions. You can accomplish this by closing your current SSH terminal window and reconnecting to your instance in a new one. Your new SSH session will have the appropriate docker group permissions.

Verify that the ec2-user can run Docker commands without sudo.

How to install and run Docker Containers on Amazon EC2 Instance?

Create, build Dockerfile, and run the Docker Container

Create a file called Dockerfile. A Dockerfile is a manifest that describes the base image to use for your Docker image and what you want to be installed and running on it.

How to install and run Docker Containers on Amazon EC2 Instance?

Edit the Dockerfile you just created and add the following content.

Build the Docker image from your Dockerfile.

How to install and run Docker Containers on Amazon EC2 Instance?

Run docker images to verify that the image was created correctly.

Run the newly built image. The -p 80:80 option maps the exposed port 80 on the container to port 80 on the host system.

Open a browser and point to the server that is running Docker and hosting your container.

If you are using an EC2 instance, this is the Public DNS value for the server, which is the same address you use to connect to the instance with SSH. Make sure that the security group for your instance allows inbound traffic on port 80.

In our case, the public IP address is: 18.141.140.63

You should see a web page with your “Hello World!” statement.


Conclusion

In this blog, we have explored the concept of Docker and containerization. We had a look at how to install docker on an EC2 instance, how to create Dockerfiles, and how to build docker images using those Dockerfiles and run the docker container which will run the Apache server. We will see how we can use Amazon ECS for launching and managing your containers. Stay tuned to keep getting all updates about our upcoming new blogs on AWS and relevant technologies.

Meanwhile …

Keep Exploring -> Keep Learning -> Keep Mastering

This blog is part of our effort towards building a knowledgeable and kick-ass tech community. At Workfall, we strive to provide the best tech and pay opportunities to AWS-certified talents. If you’re looking to work with global clients, build kick-ass products while making big bucks doing so, give it a shot at workfall.com/partner today.

Back To Top