loading...

AWS running Docker on EC2 and use Elastic Load balancer.

burhantaha profile image burhanuddin taha ・4 min read

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

once the instance is up and running and you are connected with the instance. (follow following tutorial 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 :

    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

  • Adding Docker repository to APT sources is next we have to do:
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

  • Next, we have to update the package database usual ubuntu command:
    sudo apt-get update

  • Finally, we are ready to install docker on system
    sudo apt-get install -y docker-ce

  • lets check if docker installed and give correct version
    docker --version
    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
    sudo usermod -aG docker ${USER}
    now exit the shell and close the terminal, and re-connect to the EC2 instance
    Now try to run
    docker version
    you will see something like this
    Alt Text

  • Lets install docker compose which is use for orchestrating docker services
    sudo apt install docker-compose -y
    docker-compose version
    which should return something like below

    Alt Text

    Thats it now we are ready to run docker on EC2 instance, lets create one image and run the image
    docker run hello-world
    if all is setup properly we can see following on console.

Alt Text

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

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
  • Click to Target Group
  • Create new target group
  • Select target
  • 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
  2. create loadbalancer Alt Text
  3. Now give the name of your load balancer and finally main part is to add listener.

Alt Text

  • 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 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.

Posted on by:

burhantaha profile

burhanuddin taha

@burhantaha

Full stack java and mobile application developer, strong supporter of Open source project. Football is life.

Discussion

markdown guide