DEV Community

aungkohtat
aungkohtat

Posted on

Launching an EC2 Instance with Docker for running Cruddur App

Launching an EC2 Instance with Docker for running Cruddur App

I am passionate about all things cloud ..!

a

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 and sign in.

DockerHub

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

a

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

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
Enter fullscreen mode Exit fullscreen mode

a

Uploaded my git repo

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

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
Enter fullscreen mode Exit fullscreen mode

a

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

2.

a

3.

a

4.

a

5.Manually select also for paid instance

a

6.

a

7.

a

  1. 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
Enter fullscreen mode Exit fullscreen mode

a

9.

a

10.

a

11.

a

a

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

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
Enter fullscreen mode Exit fullscreen mode

13.Modify git link and install at ec2 instance

a

14.

a

15.

a

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.

Top comments (0)