AWS

How to Install and Run Docker Containers on Amazon EC2 Instance?

Docker is a tool that provides lightweight containers which allow developers to deploy their applications in it which is useful as the applications can work efficiently in different environments. A Docker container is a software package that contains instructions for what the infrastructure will look like.

This guide will demonstrate the process to install and run a docker container on an Amazon EC2 instance.

How to Install and Run Docker Containers on Amazon EC2 Instance?

To install and run the docker container on an Amazon EC2 instance, the user needs to have an instance in the “Running” state with Amazon Linux as AMI. After that, select the instance and click on the “Connect” button to get the command from the platform which is used to create a connection to the instance:

On this page, select the “SSH client” tab to get the command from the “Example” section:

Paste the copied command on the terminal and change the path of the private key pair file:

Once the instance is connected, update the yum packages using the following command:

sudo yum update

Type the following command to install docker on the EC2 instance:

sudo yum install docker

Write-out the provided command to get the administrative permissions for the EC2 user:

sudo usermod -aG docker ec2-user

Again, install the docker from the admin to access it on the instance:

sudo yum install docker

Utilize the following command to reboot the instance, so the docker is set up in the instance:

sudo reboot

After the reboot is done, the user needs to connect to the EC2 instance using the command mentioned on the connect page of the instance which might look like the following command with some alterations:

ssh -i "C:\Users\Lenovo\Documents\PKPF.pem" ec2-user@ec2-18-136-106-167.ap-southeast-1.compute.amazonaws.com

The following is the syntax of the above command:

ssh -i [Path of Private key pair] ec2-user@<Public DNS>

After connecting to the instance, start the docker service:

sudo service docker start

Then, check if the docker is running or not:

docker info

Now, run the docker container in the detached mode which will map port 80 of the host to port 80 of the container. Here, the name and the image of the container is “nginx”:

docker run -d -p 80:80 --name nginx nginx

Type the following command to get the list of containers:

docker container ls

Once the container is available on the docker, edit the security group rules of the instance to add an “HTTP” port with “Port range 80” which will be accessible from “Anywhere” on the internet:

Once the port is open to run the docker container, simply copy the “Public DNS” from the instances page on EC2 dashboard:

Paste the Public DNS on the web browser and the docker container will be up and running:

You have successfully installed and executed the docker container on the Amazon EC2 instance.

Conclusion

To install and run the docker container on the Amazon EC2 instance, the user needs to have an EC2 instance in a running state and then connect to it. After that, install docker on it with the administrative permissions and start its services. Then, run the docker container having a Nginx image and visit it using the Public DNS of the instance.

About the author

Talha Mahmood

As a technical author, I am eager to learn about writing and technology. I have a degree in computer science which gives me a deep understanding of technical concepts and the ability to communicate them to a variety of audiences effectively.