This project demonstrates a CI/CD pipeline using GitHub Actions to build, test, and deploy a Dockerized full-stack web application. The backend is built with Flask, while the frontend leverages React. The application supports Docker Compose for streamlined development but also allows running the backend and frontend separately using individual Dockerfiles.
- Overview
- Features
- Prerequisites
- Installation
- Usage
- CI/CD Pipeline
- Project Structure
- Deployment (Optional)
- Contributing
- License
- Contact
The primary goal of this repository is to provide an example of how to integrate a Flask and React application using Docker and Docker Compose, along with a fully automated CI/CD pipeline via GitHub Actions. This setup ensures that every change is automatically tested, built, and deployed.
- Dockerized Application: Supports both Docker Compose and standalone Dockerfile-based builds.
- Docker Compose Support: Runs both the Flask backend and React frontend as separate services.
- CI/CD Pipeline: Automated workflows using GitHub Actions manage building, testing, and deployment.
- Full-Stack Integration: Seamless communication between the Flask backend and React frontend.
- Scalable Architecture: Easy to extend and customize for more complex projects.
Before running this project, ensure you have the following installed:
- Docker
- Docker Compose
- Git
- A GitHub account (for accessing GitHub Actions)
-
Clone the Repository:
git clone https://github.com/regostar/flask_react_app.git cd flask_react_app -
Start the Application using Docker Compose:
docker-compose up --build
- This command builds the backend and frontend images, runs both containers, and links them together.
-
Stop the Application:
docker-compose down
If you prefer to run the backend and frontend separately, follow these steps:
-
Build and Run the Flask Backend:
cd backend docker build -t flask-backend . docker run -p 5000:5000 flask-backend
-
Build and Run the React Frontend:
cd frontend docker build -t react-frontend . docker run -p 3000:3000 react-frontend
Note: Ensure the backend is running before starting the frontend, as the frontend fetches data from the backend.
-
Start the Application:
docker-compose up
-
Access the Application:
- Frontend: http://localhost:3000
- Backend API: http://localhost:5000
-
Rebuild after making changes:
docker-compose up --build
-
Start the backend separately:
docker run -p 5000:5000 flask-backend
-
Start the frontend separately:
docker run -p 3000:3000 react-frontend
The project includes a GitHub Actions workflow configured to:
- Build the Docker images on every commit.
- Run tests to ensure application stability.
- Deploy (or push) the Docker images on successful builds.
- The workflow file is located in the
.github/workflowsdirectory. - Modify environment variables and secrets in your repository settings for deployment.
- Customize the workflow steps as needed to suit your deployment environment.
├── backend # Flask backend code
│ ├── Dockerfile # Backend-specific Dockerfile
├── frontend # React frontend code
│ ├── Dockerfile # Frontend-specific Dockerfile
├── .github # GitHub Actions workflows
├── docker-compose.yml # Docker Compose configuration
├── README.md # Project documentation
└── ... # Additional configuration and support files
-
Authenticate with GCP:
gcloud auth login gcloud config set project [YOUR_PROJECT_ID] -
Enable required services:
gcloud services enable compute.googleapis.com -
Create a VM instance on GCE:
gcloud compute instances create flask-react-vm \ --machine-type=e2-medium \ --image-family=debian-11 \ --image-project=debian-cloud \ --tags=http-server,https-server -
SSH into the instance and install Docker:
gcloud compute ssh flask-react-vm sudo apt update && sudo apt install -y docker.io docker-compose -
Clone the repository on the VM:
git clone https://github.com/regostar/flask_react_app.git cd flask_react_app -
Run the application using Docker Compose:
docker-compose up --build -d
-
Allow external access to the instance:
gcloud compute firewall-rules create allow-http \ --allow=tcp:80,tcp:3000,tcp:5000 \ --target-tags=http-server
-
Enable required GCP services:
gcloud services enable container.googleapis.com -
Create a GKE cluster:
gcloud container clusters create flask-react-cluster \ --num-nodes=2 \ --zone=us-central1-a -
Authenticate
kubectlwith GKE:gcloud container clusters get-credentials flask-react-cluster --zone us-central1-a
-
Build and push Docker images to Google Container Registry (GCR):
docker build -t gcr.io/[YOUR_PROJECT_ID]/flask-backend:latest backend docker build -t gcr.io/[YOUR_PROJECT_ID]/react-frontend:latest frontend docker push gcr.io/[YOUR_PROJECT_ID]/flask-backend:latest docker push gcr.io/[YOUR_PROJECT_ID]/react-frontend:latest
-
Deploy to Kubernetes:
kubectl apply -f k8s/backend-deployment.yaml kubectl apply -f k8s/frontend-deployment.yaml kubectl apply -f k8s/backend-service.yaml kubectl apply -f k8s/frontend-service.yaml
-
Get the external IP:
kubectl get services
-
Access the application using the external IP assigned to the frontend service.
Contributions are welcome! To contribute:
- Fork the repository.
- Create a feature branch:
git checkout -b feature/my-new-feature
- Commit your changes:
git commit -am 'Add new feature' - Push to the branch:
git push origin feature/my-new-feature
- Open a pull request with a detailed description of your changes.
Please ensure your code adheres to the project's standards and includes tests where applicable.
This project is licensed under the MIT License.
For any questions or support, please contact renugopal17.siva@gmail.com or open an issue in this repository.
