AWS running Docker on EC2 and use Elastic Load balancer. 
Published: 2020-05-19 11:04:13

Running the docker service and docker image on EC2 instance is very simple in this brief write up i will try to cover how we can setup docker engine and then run application on the docker and then expose using Application load balancer

I have use EC2 instance with Ubuntu 18.04, one can find this in the catalog under free tier
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/c2y5pnouh5gckr5tiblf.png)

once the instance is up and running and you are connected with the instance. (follow following <a href="https://docs.aws.amazon.com/quickstarts/latest/vmlaunch/step-2-connect-to-instance.html">tutorial </a> to connect EC2 instance form your machine.)


* 
Make sure you have root access or IAMROLE to install using root user
* 
Firstly, we're going to install Docker and Docker Compose packages to the Ubuntu system. And we will be using Docker packages from the Official Ubuntu repository. The first step is to add the GPG key for the official Docker repository using :
<code>
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
</code>

* 
Adding Docker repository to APT sources is next we have to do:
<code>sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"</code>
* 
Next, we have to update the package database usual ubuntu command:
<code>sudo apt-get update</code>
* 
Finally, we are ready to install docker on system
<code>sudo apt-get install -y docker-ce</code>

* lets check if docker installed and give correct version
<code>docker --version</code>
when we run this command on EC2 instance of AWS we usually get error and required sudo to run command. 
*to avoid this we have to add our user in the group name as docker which is available by default on the AWS*
<code>sudo usermod -aG docker ${USER}</code>
now exit the shell and close the terminal, and re-connect to the EC2 instance 
Now try to run 
<code>docker version</code>
you will see something like this 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pezbztm7pzh39nmbtjbe.png)
* Lets install docker compose which is use for orchestrating docker services 
<code> sudo apt install docker-compose -y </code>
<code> docker-compose version </code>
which should return something like below
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dqauqv7oprx1liv7kzk8.png)
---
Thats it now we are ready to run docker on EC2 instance, lets create one image and run the image
<code> docker run hello-world </code>
if all is setup properly we can see following on console. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ox4ub84fz0ep16hf3ijf.png)

Hurray!!! all is done. 

Now to expose our service using Elastic load balancer, 

what i have done i have run Laravel application on Docker on EC2. 
running Laravel application using docker is not in the scope on this writeup but i will cover in dedicated blog.
so i have following  
1. Laveravel php application 
2. Ngnix running on docker and contain Laravel appliaction as root.

when all services are up and running on EC2 instance.something like this for me as i am running Nginx under docker. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6xxwc6xsbzvnzrxtf9bp.png)

Lets move the expose the docker service running on EC2 using Elastic Loadbalancer. 

- Create target group, target group is use to route the traffic from loadbalancer to EC2 instance based on rule define on loadbalancer level.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dlq4u2m61k263f7rbc3q.png)
1. Click to Target Group
2. Create new target group
3. Select target 
4. Edit and add EC2 instance in this target group on port 80

what it says is that "in this target group there is one EC2 instance which is listening to port 80 to serve any request". 
Now if you see above as said i have nginx see i have port 80 on which nginx server is listening with php application. 

Now we need loadbalancer which will help to route external traffic to our instance. 

AWS provide two different flavour of loadbalancer 
1. Application load balancer (which we will use)
2. Network load balancer (use when you need to control traffic, need static ip for your application or want very high performance) 

To enable loadbalancer 

1. click on loadbalancer on EC2 console something like this
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bnf5cwub6x4j699jmub7.png)
2. create loadbalancer
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/i4uq9b38zpbxavpaq0uo.png)
3. Now give the name of your load balancer and finally main part is to add listener.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vlc4o8whb3uin7eq6fy4.png)
- Chose protocol http/https
- Chose port on which load balancer will listen
- And finally define rule like forward request to already created target group (as we did above)
- Finally the main part, either create or choose the security group which has inbound rule where above port is open for all incoming traffic. something like this
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6z4pksv1x1gynlui8dlz.png)
if this is not present then you will not able to access load balancer url from outside as from outside ports couldn't be reachable. 

Thats it, now copy the dns name or public ip of your loadbalancer and try to access using browser, you will see application index page loaded which is running in docker on EC2 instance.