Skip to content

Deploy Application on AWS with GitHub Action and DockerHub

πŸ“ Outline:

  1. What is AWS EC2 and GitHub Actions? πŸ€”
  2. Setting up AWS EC2 Instance πŸ’»
  3. Pushing Docker Images to DockerHub πŸ“¦
  4. Automating CI/CD with GitHub Actions πŸ”„
  5. Deploying Docker Image on AWS EC2 🐳
  6. Connecting EC2 with a Database Service (e.g., MySQL) πŸ—„οΈ
  7. Example: Running a Node.js App with a MySQL Database 🎯

1. What is AWS EC2 and GitHub Actions? πŸ€”

Amazon EC2 (Elastic Compute Cloud) is a cloud service that provides scalable compute capacity, allowing you to run applications and services in a secure, isolated environment. GitHub Actions is a CI/CD tool that automates software workflows directly in GitHub.

With GitHub Actions, you can automate building, testing, and deploying your application to AWS EC2 after pushing to GitHub.


2. Setting Up AWS EC2 Instance πŸ’»

To deploy your app on AWS, you first need an EC2 instance:

  1. Launch an EC2 Instance:
  2. Go to AWS EC2 Console.
  3. Choose an Ubuntu server.
  4. Select instance type (e.g., t2.micro).
  5. Configure security groups (allow HTTP, HTTPS, and SSH).
  6. Connect to the Instance via SSH:
    ssh -i "your-key.pem" ubuntu@ec2-your-instance-ip
    

3. Pushing Docker Images to DockerHub πŸ“¦

To deploy a Docker container on EC2, you first need to push your Docker image to DockerHub.

  1. Login to DockerHub:
docker login
  1. Build Docker Image:
docker build -t yourusername/appname .
  1. Push to DockerHub:
    docker push yourusername/appname
    

4. Automating CI/CD with GitHub Actions πŸ”„

Set up GitHub Actions to automate building, testing, and pushing Docker images to DockerHub.

  1. Create a .github/workflows/ci.yml file:
name: CI Pipeline

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1

      - name: Build and Push Docker image
        uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          tags: yourusername/appname:latest

      - name: Login to DockerHub
        run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
  1. Push to GitHub: When you push code to the main branch, GitHub Actions will build and push the Docker image to DockerHub.

5. Deploying Docker Image on AWS EC2 🐳

Once the Docker image is available on DockerHub, you can pull and run it on your EC2 instance.

  1. Install Docker on EC2:
sudo apt update
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker
  1. Pull the Docker Image:
docker pull yourusername/appname:latest
  1. Run the Docker Container:
    docker run -d -p 80:3000 yourusername/appname
    

6. Connecting EC2 with a Database Service πŸ—„οΈ

For a full deployment, you might want to connect your application to a database, such as MySQL.

  1. Create a MySQL Docker Container:
docker run -d \
--name mysql-db \
-e MYSQL_ROOT_PASSWORD=rootpassword \
-e MYSQL_DATABASE=appdb \
-e MYSQL_USER=user \
-e MYSQL_PASSWORD=password \
-p 3306:3306 \
mysql:latest
  1. Update Application Configuration: Update your app’s environment variables to point to the MySQL database.
    DATABASE_HOST=mysql-db
    DATABASE_USER=user
    DATABASE_PASSWORD=password
    

7. Example: Running a Node.js App with MySQL Database 🎯

Here’s how to run a simple Node.js app with MySQL on EC2 using Docker.

Step 1: Create docker-compose.yml:

version: "3"
services:
  app:
    image: yourusername/appname
    ports:
      - "80:3000"
    environment:
      DATABASE_HOST: mysql-db
      DATABASE_USER: user
      DATABASE_PASSWORD: password
    depends_on:
      - mysql-db
  mysql-db:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: appdb
      MYSQL_USER: user
      MYSQL_PASSWORD: password
    ports:
      - "3306:3306"

Step 2: Deploy the App:

  1. SSH into EC2 and run:
    docker-compose up -d
    

Your application should now be live and connected to a MySQL database! πŸŽ‰


πŸš€ Summary Table:

Step Action
1 Launch EC2 and connect via SSH
2 Push Docker Image to DockerHub
3 Setup GitHub Actions for CI/CD
4 Pull Docker Image on EC2 and Run
5 Connect App to MySQL Database