How to launch EC2 instance in AWS using Docker-Machine

docker
Reading Time: 4 minutes

Hello Readers! We are back again. And In this blog we will learn how we can launch an EC2 instance in AWS using Docker-Machine in a few easiest steps. We have some earlier blogs on EC2 instance which we can refer to are: https://blog.knoldus.com/how-to-launch-ec2-instance-from-aws-cli/ & https://blog.knoldus.com/how-to-create-ec2-instance-using-python3-with-boto3/

So, before starting we again have some prerequisites that we need to follow. These are the following:

  1. You must have configured AWS CLI. Check it by this command.

$ aws configure

configure
  1. You must have docker-machine installed on your system. If you hadn’t and want to install it then follow these steps for installing:
$  curl -L https://github.com/docker/machine/releases/download/v0.13.0/docker-machine-`uname -s`-`uname -m` -o ~/docker-machine
docker
$ chmod +x ~/docker-machine
$ sudo cp ~/docker-machine /usr/local/bin/

It’s done and now you can check the docker-machine version.

$ docker-machine –version
docker-machine
$ which docker-machine
docker-machine

Currently we don’t have any machines. You can check it by using this command.

$ docker-machine ls
machine

Creating EC2 Instance:

We have successfully installed docker-machine on our system. We will now launch an EC2 instance using docker-machine by a single command. In this command I will be using driver amazonec2 for creating it. 

If you want to look at the options which you can pass by using this driver then use this command:

$ docker-machine create --driver amazonec2 --help | less
ec2

I have logged into my AWS Console and you can see here I dont have any EC2 right now.

aws

Lets launch the instance. Use the following command for launching.

$ docker-machine create --driver amazonec2 --amazonec2-instance-type "<TYPE>" --amazonec2-region "<REGION>" <INSTANCE_NAME>
create ec2

Here I am using instance type as t2.micro which is free tier eligible. And I am creating the instance in the ap-south-1 region which is mumbai.This command will take some time to create the instance. It will launch a security group, a new key pair and an EC2 instance.

terminal

It’s done now. Lets move to the AWS console and check if an instance got created.

ec2

Yaa its creating and one instance is up and running. You can see it here also.

ec2

You can see it here also by listing docker-machine.

$ docker-machine ls
docker-machine

It has created a key pair on the name of ec2 instance.

key-pair

It has created a security group you can see here.

sg

Connect to the EC2 Instance:

We can easily ssh into the instance by only a simple command. Use the following command for this.

$ docker-machine ssh aws1
ssh

Now, we are inside our EC2 instance running in AWS.

Terminate the EC2 Instance:

If you want to terminate your running instance in AWS then you use the following command:

$ docker-machine rm aws1
terminate

You can see the result here. The instance aws1 got terminated.

ec2

It will delete all the resources that it has created.

Congrats! 👏 We are successfully done now!

Conclusion

Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs. Follow me to get updates on different technologies.

HAPPY LEARNING!

Written by 

Naincy Kumari is a DevOps Consultant at Knoldus Inc. She is always ready to learn new technologies and tools. She loves painting and dancing.