Photo by Rubaitul Azad on Unsplash
A Step-by-Step Guide To Deploy Docker Containers On AWS EC2 Instance
This article shows you how to deploy your Docker container or image onto an AWS EC2 instance. Without further ado, here’s everything you need to know: from basic EC2 and docker setup to running your first container.
Docker and containers have gained tremendous popularity in different industries because of their flexibility and efficiency. How to Deploy Docker Images on an EC2 Instance
We will manually spin EC2 instance and install Docker. After we set up docker, we will run a docker image.
Step 1: Creating your EC2 instance
Go to EC2 in the AWS Console. In AWS Management Console, search “EC2” and select the service.. Start the instance creation process, click Launch Instance.
Select an Amazon Machine Image (AMI) :- Choose an Amazon Linux AMI with pre-installed configurations for the operating system and application server, and applications needed to be deployed.
Select an Instance Type:- Select an instance type, like t2. micro is designed for small-scale deployments.
Configure Security Group :- Configure your security group rules to control your instance’s incoming and outgoing traffic. In this case, Create rules to allow incoming traffic over HTTP (port 80) and HTTPS (port 443).
Set Up a Key Pair :- Generate and download a secure SSH key pair to access your instance.
Launch Your Instance :- Store your key file in a safe location and now start the instance.
Go to the EC2 Instances page to get the public IP for your instance
Step 2:- DOCKER INSTALL ON EC2 INSTANCE
SSH into the Instance :- To connect into your EC2, open terminal and type:
ssh -i keypair. pem ec2-user@
Install Docker :- Install and start up Docker by running the commands below.
sudo yum install -y docker
sudo service docker start
Verify Installation :- View the status and configuration of Docker with
docker info
STEP 3:- Execute a docker image
Run a Sample Docker Image
Run the Sample Node with the following command js application:
docker run -d -p 80:8080 mahiratechnology/docker-demo:1
The -p 80:8080 flag maps port 8080 on the container to port 80 on the EC2 instance.
Test the Deployment
Test the application in a browser using the EC2 instance’s public DNS name. If it works, you’ll get the “Hello World! message.
Conclusion :-
You have deployed a Docker container on an AWS EC2 instance! This approach is the base for deploying more complex applications using the techniques outlined in Docker and AWS Happy deploying! 🚀