How to create EC2 instance and install docker in AWS

Docker and AWS are just BOOOOOMMMM!! for me. Everything is damn fast with docker and now I don’t have to waste my laptop’s computing power when I have AWS.

Launching an EC2 Instance

I’ll tell you how to launch an instance in AWS and install docker in it. You just need a free AWS account (which I’ll be using to demonstrate this exercise).

To run an instance you require an vpc (virtual private cloud). But I will do it while creating the instance.

  • Login to your AWS management console
  • Click on services and under compute choose EC2.
  • Under Instance list click on Instance.
  • And then click on Launch Instance.

Don’t worry about that docker_node1 .

  • Here you will be asked to select an AMI. Just select the Amazon Linux 2 AMI (HVM), SSD Volume Type.
  • Now choose free tier eligible.
  • Now click next.
  • Then click on create new vpc. Just don’t worry about other things and move forward.
  • Then click on create vpc.
  • Then fill the details as follows, you can also choose different ipv4 range as suitable.
  • Then hit create and then close.
  • Now you will see an vpc is created with name vpc1.
  • Now go back to where you left your instance.
  • Select your vpc and now we also require a subnet.
  • Click on create a subnet and fill in details as follows.
  • Hit create and then close. Now you will see a subnet name subnet1 is created.
  • Go back where you left.
  • Select your vpc and subnet.
  • Now enable the auto-assign pubic ip option.
  • Now just do next until the 6th step. In 6th step it will create a security group or use can use existing security group.
  • Now review and launch.
  • Now it will ask you to create a key pair. Select create a new key pair give it a name.
  • And finally Launch.

You are done with creating your instance. You can easily connect to your EC2 instance by navigating to connect and you can also find it on https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstances.html , I am using browser-based SSH connection.

Installing Docker

Next step is to install docker on your EC2 instance. Navigate to your terminal and enter below given commands:

[ec2-user@ip-172-31-1-247 ~]$ sudo update -y
#this cmd will update your system
[ec2-user@ip-172-31-1-247 ~]$ sudo yum intall docker -y
#this will install docker community edition into your system
[ec2-user@ip-172-31-1-247 ~]$ sudo systemctl status docker
#this command is use to check the status of any service you will see something like 'Active: inactive'
#this means your docker in not started yet
[ec2-user@ip-172-31-1-247 ~]$ sudo systemctl start docker
#this will start your docker deamon
[ec2-user@ip-172-31-1-247 ~]$ sudo systemctl status docker
#to check again the service now 'Active: active' this means service started correctly

We now add ec2-user to docker group by usermod command. To make it simple by doing the below command we don’t have to use ‘sudo’ again and again.

[ec2-user@ip-172-31-1-247 ~]$ sudo usermod -aG docker ec2-user
[ec2-user@ip-172-31-1-247 ~]$ exit

Now again login to your instance and type command without ‘sudo’.

[ec2-user@ip-172-31-1-247 ~]$ systemctl status docker
[ec2-user@ip-172-31-1-247 ~]$ docker info

Now you have successfully started docker. It’s time to run a container. I am running a nginx server on port 80 of EC2 instance and on port 80 inside the image.

[ec2-user@ip-172-31-1-247 ~]$ docker container run -itd -p 80:80 nginx
#what this command do is that it pulls down the respective image if unable to find locally and runs it #simultaneously 
#docker has changed some commands like before it was 'docker run' and now it's 'docker container run' #both the commands will work fine. You can always use 'docker container run --help' to understand it's #internals 
[ec2-user@ip-172-31-1-247 ~]$ docker container ls
#this will list number of containers running

Now to check whether your nginx container is running or not.

[ec2-user@ip-172-31-1-247 ~]$ curl https:<your-ip>:80
#to see your ip just type 'ifconfig' under eth0 inet will bw your ip

If you see something like this, then congrats you successfully ran a nginx docker container in your EC2 instance.

Conclusion

Important: You probably should shutdown all the tasks and EC2 instance you created during this tutorial and review all work done on AWS so you don’t get charged for it in any way.

There is nothing much to say though it’s my first blog and I just love using container and AWS as both looks fantastic to me. Docker environment is growing at an astonishing rate and soon will replace the heavy servers to light weight containers. It may look uncanny to use, but, as you once get a hang of it you will find everything surfing.

If you have come up this far please leave a review or comment. It means a lot to me.

2 thoughts on “How to create EC2 instance and install docker in AWS

Leave a comment

Design a site like this with WordPress.com
Get started