Docker GitHub Actions Deployment
This project demonstrates how to automate CI/CD for a Dockerized Python Flask application using GitHub Actions. The project uses tools like Git, GitHub, Python, Flask, Pytest, Docker, and Docker Hub to create a seamless deployment pipeline.
π Project Overview
The goal of this project is to set up a complete CI/CD pipeline that automates the build, test, and deployment process for a Dockerized Python Flask application whenever code changes are pushed to the main branch.
π οΈ Tools & Technologies Used
- Python: Programming language for developing the Flask application.
- Flask: Lightweight web framework for building the web application.
- Pytest: Testing framework for writing unit tests.
- Git & GitHub: Version control and source code management.
- GitHub Actions: Automates CI/CD workflows.
- Docker: Containerizes the application for consistent deployment.
- Docker Hub: Repository for storing and distributing Docker images.
- Anaconda: Manages Python environments.
- Visual Studio Code (VS Code): Code editor for development.
π Pre-requisites
Before you begin, ensure you have the following installed on your system:
- Python 3.9
- Anaconda (for managing Python environments)
- Git (for version control)
- Docker (for containerization)
- Visual Studio Code (VS Code) (recommended code editor)
- Docker Hub Account (for pushing Docker images)
ποΈ Project Structure
DOCKERGITHUBACTIONSDEPLOYMENT/
βββ .github/
β βββ workflows/
β βββ cicd.yml # GitHub Actions workflow configuration
βββ venv/ # Virtual environment (typically ignored in version control)
βββ .gitignore # Specifies files to ignore in Git (e.g., venv, temporary files)
βββ app.py # Main application code (Flask app)
βββ Dockerfile # Docker configuration for containerizing the application
βββ LICENSE # License for the project
βββ README.md # Project documentation
βββ requirements.txt # Python dependencies required by the project
βββ test_app.py # Unit tests for the Flask application
πΊοΈ Architecture Diagram
+-----------------+
| Developer |
+-----------------+
|
| 1. Push Code (GitHub)
v
+-----------------+
| GitHub Repo |
| (Source Control)|
+-----------------+
|
| 2. Trigger GitHub Actions
v
+--------------------------+
| GitHub Actions |
| (CI/CD Pipeline) |
+--------------------------+
| | |
(Build) | (Test) (Dockerize)
| | |
v v v
+-------------------------------+
| Docker Image Build |
+-------------------------------+
|
| 3. Push Docker Image
v
+-----------------+
| Docker Hub |
+-----------------+
|
| 4. Deploy Container
v
+-----------------+
| Local/Cloud |
| Environment |
+-----------------+
π Getting Started
1. Clone the Repository
git clone https://github.com/kala-techies/DockerGithubActionsDeployment.git
cd DOCKERGITHUBACTIONSDEPLOYMENT
2. Set Up Local Environment
-
Create and Activate Conda Environment:
-
Install Dependencies:
pip install -r requirements.txt
-
Run the Flask Application Locally:
python app.py
-
Run Unit Tests:
pytest
π¦ Dockerization
Manual Way to Build and Publish Docker Image
The manual process is useful for development and testing purposes, allowing you to control each step before automating the pipeline.
-
Build Docker Image:
docker build -t flaskapp .
-
Run Docker Container:
docker run -p 5000:5000 flaskapp
-
Push Docker Image to Docker Hub:
docker tag flaskapp:latest yourdockerhubusername/flaskapp:latest
docker push yourdockerhubusername/flaskapp:latest
- This step allows you to share your Docker image with others via Docker Hub.
Automated Way (CI/CD Pipeline)
The automated process uses GitHub Actions to handle the build, test, and deployment steps whenever changes are pushed to the repository. This ensures consistency and saves time by eliminating manual steps.
π CI/CD Pipeline (GitHub Actions)
Pipeline Stages Explained
-
Build Stage:
- Purpose: Checks out the code and builds a Docker image to ensure the application is containerized correctly.
- Why: This step verifies that the Dockerfile is correctly configured and the application can be packaged.
-
Test Stage:
- Purpose: Sets up Python, installs dependencies, and runs unit tests using Pytest.
- Why: Ensures the application is functioning as expected before deployment.
-
Publish Stage:
- Purpose: Logs into Docker Hub, builds a fresh Docker image, and pushes it to Docker Hub.
- Why: Automates the deployment process, allowing others to pull and use the Docker image.
GitHub Actions Workflow (.github/workflows/cicd.yml):
name: CI/CD for Dockerized Flask App
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
dockerbuild:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build Docker Image
run: docker build . --file Dockerfile --tag workflow-test:$(date +%s)
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install dependencies
run: pip install -r requirements.txt
- name: Run tests
run: pytest
build-and-publish:
needs: build-and-test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/flaskapp:latest
π Usage
This project is ideal for:
- Learning CI/CD: Understand how to automate build, test, and deployment processes using GitHub Actions.
- Dockerization: Learn how to containerize Python applications using Docker.
- Testing: Get hands-on experience with writing and running tests using Pytest.
- Deployment: Automate deployment to Docker Hub and run containers in a consistent environment.
π€ Contributing
We welcome contributions to improve this project! If you're interested in adding features, fixing bugs, or improving documentation:
- Fork the repository.
- Create a new branch:
git checkout -b feature-branch
- Make your changes and commit:
git commit -m 'Add new feature'
- Push to your branch:
git push origin feature-branch
- Open a pull request.
For any questions or suggestions, reach out at connectwithkala@gmail.com.
π License
This project is licensed under the MIT License. See the LICENSE file for details.