Let’s see the steps to install docker on EC2. First, you will need to have an AWS cloud account, you can sign-up for a free AWS account here.
Creating an EC2 Instance
Let’s start with creating an EC2 instance. I will launch an EC2 instance in the “us-east-1” region. Here is the direct link to jump to launch an EC2 instance.
Name: Give a suitable name to the EC2 Instance. I am naming it “web-hosting”
Application and OS Images (Amazon Machine Image): Select “Amazon Linux 2023 AMI” or any other suitable AMI based on your liking.
Architecture: Let’s keep it to “64-bit (x86).
Instance type: t2.micro (this is eligible for Free tier)
Key pair (login): Create a new key pair or select the existing key pair.
Network settings:
- Select Allow SSH traffic from anywhere.
- Select Allow HTTPS traffic from the internet.
- Select Allow HTTP traffic from the internet
Storage: I am creating this instance only for the demo purpose, so i am keeping it 8GB, but please feel free to change the storage size to what’s suitable for your website.
That’s it, we are pretty much done with the configuration and can now hit “Launch Instance”. AWS will take a few seconds before our instances show in the running state.
Login to EC2 and Install Docker
Login to the EC2 instance, we can do that by using the following command “ssh -i /path/to/your/private/key ec2-user@<public-ip-address-of-ec2>”
We can install “docker” on Amazon Linux using the below command.
Update Package Repository
sudo yum update -y
Install Docker
sudo yum install docker -y
Enable Docker Service to Auto Startup on Server Boot
sudo systemctl enable docker
Start Docker
sudo systemctl start docker
Check Docker Service Status
sudo systemctl status docker
Verify Docker Installation
sudo docker --version
Run Docker Hello World Container
sudo docker run hello-world
Adding current users to the Docker Group
As you can see, we are running docker commands as “sudo”. We need to do this because the current user (ec2-user) does not have permission to manage docker services. We can add the current user to the Docker Group by running the below command.
sudo usermod -aG docker $(whoami)
After this, we can run docker services without “sudo”. For example, let’s run “docker –version”. It should run.
Frequently Used Docker Commands
Since you have successfully installed Docker the next aim would be to manage Docker service. Here is a Docker Cheat Sheet with the top 60 Frequently Used Docker Commands.