CI/CD Pipeline: Dockerized App Deployment to AWS EC2

Kaustav  Dey

Kaustav Dey

CI/CD Pipeline: Dockerized App Deployment to AWS EC2 using GitHub Actions

This project demonstrates a full CI/CD pipeline setup using GitHub Actions, Docker, and AWS EC2. Every time you push code to GitHub, the pipeline automatically builds the app, transfers it to an EC2 instance, and deploys it using Docker.

Architecture Overview

GitHub Repo


GitHub Actions (CI/CD)

├── Build Docker Image
├── SCP files to EC2
└── SSH into EC2 → Run Docker container

AWS EC2 Instance (App deployed)

Stack Used

Docker – Containerize the application
GitHub Actions – Automate CI/CD
EC2 (Ubuntu) – Host the application
SSH – Secure connection to EC2
Nginx/PM2 – Serve the app (optional)

Features

Dockerfile to containerize the app
GitHub Actions workflow to:
Build and test
Copy files to EC2
SSH and deploy using Docker
Uses GitHub Secrets for secure keys and variables

Getting Started

1. Initializing The Ec2 Instance using Terraform
terraform init
terraform plan -var 'key_pair=your-key-name'
terraform apply -var 'key_pair=your-key-name'
Set enable_elb = true to deploy with an ELB.
2. Clone the Repo
git clone https://github.com/yourusername/cicd-ec2-docker.git
cd cicd-ec2-docker
3. Fill in Secrets (GitHub Settings → Secrets)
Key Description EC2_HOST EC2 public IP or DNS EC2_USER Usually ubuntu EC2_SSH_KEY Your private key (PEM), base64-encoded EC2_PATH Path to deploy files (e.g., /home/ubuntu/app) DOCKERHUB_USERNAME Your Docker Hub username DOCKERHUB_TOKEN Your Docker Hub access token (stored as a secret)

4. Deploy Pipeline

Each push to main branch triggers:
.github/workflows/deploy.yml
Which does:
Checkout the code
Build Docker image
Copy files to EC2 using scp
SSH into EC2 and run:
docker stop old_container
docker rm old_container
docker run -d ...

Project Structure

.
├── .github/workflows/deploy.yml # GitHub Actions pipeline
├── terraform/
│ └── main.tf
│ └── variables.tf
│ └── outputs.tf
├── Dockerfile # Builds app container
├── app/ # Your Node.js / Python app
├── scripts/
│ └── deploy.sh # Script to run on EC2
├── .env.example # Sample .env structure
├── README.md

Access the App

After deployment:
http://<EC2-Public-IP>:<Port>
Make sure the port is open in your EC2 Security Group.

Local Development

docker build -t my-app .
docker run -p 3000:3000 my-app

Author

Kaustav Dey
Like this project

Posted May 18, 2025

Set up CI/CD pipeline for Dockerized app deployment to AWS EC2 using GitHub Actions.