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:
Type the following command to install docker on the EC2 instance:
Write-out the provided command to get the administrative permissions for the EC2 user:
Again, install the docker from the admin to access it on the instance:
Utilize the following command to reboot the instance, so the docker is set up in the instance:
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:
The following is the syntax of the above command:
After connecting to the instance, start the docker service:
Then, check if the docker is running or not:
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”:
Type the following command to get the list of containers:
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.