Deploy Application on AWS with GitHub Action and DockerHub
π Outline:
- What is AWS EC2 and GitHub Actions? π€
- Setting up AWS EC2 Instance π»
- Pushing Docker Images to DockerHub π¦
- Automating CI/CD with GitHub Actions π
- Deploying Docker Image on AWS EC2 π³
- Connecting EC2 with a Database Service (e.g., MySQL) ποΈ
- 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:
- Launch an EC2 Instance:
- Go to AWS EC2 Console.
- Choose an Ubuntu server.
- Select instance type (e.g., t2.micro).
- Configure security groups (allow HTTP, HTTPS, and SSH).
- Connect to the Instance via SSH:
3. Pushing Docker Images to DockerHub π¦
To deploy a Docker container on EC2, you first need to push your Docker image to DockerHub.
- Login to DockerHub:
- Build Docker Image:
- Push to DockerHub:
4. Automating CI/CD with GitHub Actions π
Set up GitHub Actions to automate building, testing, and pushing Docker images to DockerHub.
- 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
- 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.
- Install Docker on EC2:
sudo apt update
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker
- Pull the Docker Image:
- Run the Docker Container:
6. Connecting EC2 with a Database Service ποΈ
For a full deployment, you might want to connect your application to a database, such as MySQL.
- 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
- Update Application Configuration: Update your appβs environment variables to point to the MySQL database.
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:
- SSH into EC2 and run:
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 |