Launching an EC2 Instance with Docker for running Cruddur App
Tags: aws, docker, ec2, gitpod
Published: 2023-10-22 14:58:46


## Launching an EC2 Instance with Docker for running Cruddur App

I am passionate about all things cloud ..!

![a](https://cdn-images-1.medium.com/max/13824/1*cOsz6IiFqZF6JJnQ0jEvIA.png)
>  **This article is part of my cloud journey with aws bootcamp by Andrew Brown.**

## Let’s Begin!!

## 1. Push Docker Images into the DockerHub

### 1.Create a DockerHub Account

The first step is to create a DockerHub account if you don’t already have one. Visit [DockerHub](https://hub.docker.com/) and sign in.

![DockerHub](https://cdn-images-1.medium.com/max/3840/1*LmXl9QHdgX0aek9MW34kaQ.jpeg)

2.Generate an Access Token

Upon logging in to your DockerHub account, you’ll land on the dashboard. To create an Access Token, simply click on your profile picture located in the top right corner, triggering a dropdown menu to appear.

3.Log in to Docker Account

In your Gitpod terminal, use the following command to log in to your Docker account, replacing “alexiscloud” and the access token with your credentials.

Profile >> Account settings >> Security >> New Access token

Generate new token

![a](https://cdn-images-1.medium.com/max/3840/1*sclzI_YVTUxPbqpFd0p8kg.jpeg)

![a](https://cdn-images-1.medium.com/max/3840/1*vi1lQI9Qb8vEBricoUy9_A.jpeg)

### Log in to Docker Account with gitpod terminal

In your Gitpod terminal, log in to your Docker account using the following command, replacing “akhlab” and the access token with your credentials.

![a](https://cdn-images-1.medium.com/max/2994/1*B-W3O-XN_DRU0umuEqTO_Q.jpeg)

To upload the docker images without giving any tag (by default it uses the “Latest” tag) run the following commands

     docker image tag aws-bootcamp-cruddur-2023-backend-flask:latest  akhlab/cruddur-backend

     docker image tag aws-bootcamp-cruddur-2023-frontend-react-js:latest akhlab/cruddur-frontend

     docker image push akhlab/cruddur-backend:latest

     docker image push akhlab/cruddur-frontend:latest

![a](https://cdn-images-1.medium.com/max/2602/1*zZcCSdAF0nJemFH-5_rjQA.jpeg)

![Uploaded my git repo](https://cdn-images-1.medium.com/max/3812/1*BEmyMOjcd0YNFs0VXaU2pA.jpeg)

Your DockerHub account will display these images in the Repositories section

### 2. Create new git Repositroy

Provide the new repository with the name ‘docker-compose-cruddur,’ set it to ‘Public,’ and click ‘Create repository.

![a](https://cdn-images-1.medium.com/max/2894/1*vqpnGLnKaEjSubvSTZthyQ.jpeg)

Create a new file and name it docker-compose.yml and add this code to your file and click on commit.

     version: "3.8"
     services:
       backend-flask:
         environment:
           FRONTEND_URL: "http://${ips}:3000"
           BACKEND_URL: "http://${ips}:4567"
         image: akhlab/cruddur-backend
     #    build: ./backend-flask
         ports:
           - "4567:4567"
         volumes:
           - ./backend-flask:$HOME/backend-flask
       frontend-react-js:
         environment:
           REACT_APP_BACKEND_URL: "http://${ips}:4567"
         image: akhlab/cruddur-frontend
     #    build: ./frontend-react-js
         ports:
           - "3000:3000"
         volumes:
           - ./frontend-react-js:$HOME/frontend-react-js
    
     # the name flag is a hack to change the default prepend folder
     # name when outputting the image names
     networks: 
       internal-network:
         driver: bridge
         name: cruddur

![a](https://cdn-images-1.medium.com/max/3828/1*RmBzDa9aTLD8gn7ZN7M87g.jpeg)
>  **Change yout dockerhub image name.**

### 3.Create EC2 instance

Login aws account and creating new ec2 instance using keypair and t2.micro free tier.

1.

![a](https://cdn-images-1.medium.com/max/3836/1*JpfbJ5rdG3TwEJjyWIoKFg.jpeg)

2.

![a](https://cdn-images-1.medium.com/max/3820/1*msFjLql9kUla4ZsFnpWKbQ.jpeg)

3.

![a](https://cdn-images-1.medium.com/max/3840/1*5F7rF1sqiWLN-uYn1SYVUQ.jpeg)

4.

![a](https://cdn-images-1.medium.com/max/3668/1*9zwYyywBLNGsj_Yyb7ajpQ.jpeg)

5.Manually select also for paid instance

![a](https://cdn-images-1.medium.com/max/3840/1*q0fDlmyqX8ZP1tf3tBNKBQ.jpeg)

6.

![a](https://cdn-images-1.medium.com/max/3838/1*0j4lGitZR_4yir7oDF-aVg.jpeg)

7.

![a](https://cdn-images-1.medium.com/max/3832/1*qfRdb4mB-gjcjEwDdLhQ5w.jpeg)

8. Next, navigate to ‘Advanced details,’ scroll to the bottom, and paste the code into the ‘User data’ section for Docker installation and service provisioning.

Remember to press ENTER to ensure the seamless execution of the provided commands.

     #!/bin/bash
    
     # This will update the ec2 instance 
     sudo yum update -y
    
     # This will install docker into ec2 instence
     sudo yum install docker -y
    
     # This will install pythong3-pip to install docker compose
     sudo yum install python3-pip
    
     # This will install docker-compose
     sudo pip3 install docker-compose
    
     # This specifies that docker service will run every time when this ec2 instence starts
     sudo systemctl enable docker.service
    
     # This command will run the docker services
     sudo systemctl start docker.service
    
    

![a](https://cdn-images-1.medium.com/max/3840/1*3lpbZ7Zi1noj9UGESOglXg.jpeg)

9.

![a](https://cdn-images-1.medium.com/max/3840/1*jn1oQERkXhl4lYvaRFGvEA.jpeg)

10.

![a](https://cdn-images-1.medium.com/max/3840/1*iRSAVLY1RCbmdQ5OC9CiIg.jpeg)

11.

![a](https://cdn-images-1.medium.com/max/3840/1*1VWlauCCY8F-h6HcKyNdsg.jpeg)

![a](https://cdn-images-1.medium.com/max/3828/1*PqdMpl8l-o8HT4vUCFsu0w.jpeg)

12.

Set up three inbound rules as follows and save them:

 1. Type: Custom TCP, Port: 3000, Source: Custom

 2. Type: SSH, Port: 22, Source: Custom

 3. Type: Custom TCP, Port: 4567, Source: Custom

These rules control incoming traffic to your AWS instance.

![a](https://cdn-images-1.medium.com/max/3836/1*8JTceELZZHeSVYY1Lcj3Rw.jpeg)

Then once again click on connect ec2 and running following script

    # To give ec2-user permission to use docker without root (BEST PRACTICE)
    sudo usermod -a -G docker ec2-user
    id ec2-user
    newgrp docker
    
    # This environment variable will be used by docker-compose file 
    export ips=$(curl http://169.254.169.254/latest/meta-data/public-ipv4)
    
    # Volumes for frontend and backend
    mkdir $HOME/backend-flask
    mkdir $HOME/frontend-react-js
    
    # To download docker-compose file directly from my github repo (PS IT IS MY GITHUB REPO)
    sudo yum install git -y
    git clone https://github.com/ohmykloud/docker-compose-cruddur.git
    
    # Gitclone command will make a directory/ changing the directory 
    cd docker-compose-cruddur
    
    # Putting the file to home directory
    mv docker-compose.yml $HOME/docker-compose.yml
    
    # Changing the directory to Home
    cd ..
    
    # RUN THE CONTAINERS TO ENJOY CRUDDER INSIDE EC2
    docker-compose -f /home/ec2-user/docker-compose.yml up -d

13.Modify git link and install at ec2 instance

![a](https://cdn-images-1.medium.com/max/3832/1*OIb5D8K_rPgo4lmTNVfp0g.jpeg)

14.

![a](https://cdn-images-1.medium.com/max/3834/1*AJvEOCHrtJwTOzK6cv-Wwg.jpeg)

15.

![a](https://cdn-images-1.medium.com/max/3840/1*QviQAtqojjvyXh11AREWUA.jpeg)

Run ‘docker-compose -f /home/ec2-user/docker-compose.yml up -d ’ and you’ll see a successful launch confirmation. To access the Cruddur App, open a new tab in your web browser and paste the public IP address into the URL bar.