You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You push code to GitHub
↓
GitHub Actions runs automatically
↓
❌ Tests fail → stops, tells you the bug → app stays safe
✅ Tests pass → builds Docker image → ready to deploy
What is Docker?
WITHOUT DOCKER
Your Windows PC → Cloud Server (Linux) = ❌ breaks
Different Python versions, different libraries
WITH DOCKER
Pack everything into one box 📦
Your code + Python 3.11 + all libraries
Same box runs on ANY machine ✅
How They Work Together
You write code
↓
git push ← only thing YOU do
↓
CI/CD automatically:
✅ Runs tests
✅ Builds Docker image
✅ Deploys to cloud (when configured)
↓
🌍 Live API — zero manual work
🔗 API Endpoints
Endpoint
What it does
Example
GET /
Returns welcome message
{"message": "Welcome to Sentiment Analyzer API!"}
GET /analyze?text=your text
Analyzes sentiment of text
{"sentiment": "positive", "emoji": "😊"}
😊 Sentiment Analyzer in Action
Input → "I love coding!"
Output → {"sentiment": "positive", "confidence": "95%", "emoji": "😊"}
Input → "This is terrible!"
Output → {"sentiment": "negative", "confidence": "89%", "emoji": "😞"}
Input → "Today is okay"
Output → {"sentiment": "neutral", "confidence": "0%", "emoji": "😐"}
fromfastapi.testclientimportTestClientfrommainimportappclient=TestClient(app)
deftest_home():
response=client.get("/")
assertresponse.status_code==200deftest_positive_sentiment():
response=client.get("/analyze?text=I love this!")
assertresponse.json()["sentiment"] =="positive"deftest_negative_sentiment():
response=client.get("/analyze?text=I hate this!")
assertresponse.json()["sentiment"] =="negative"
Dockerfile — Docker Instructions
FROM python:3.11 # use Python 3.11 as base
WORKDIR /app # set working folder inside container
COPY requirements.txt . # copy requirements first
RUN pip install -r requirements.txt # install all dependencies
COPY . . # copy all your code
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
requirements.txt — Dependencies
fastapi ← web framework
uvicorn ← server to run FastAPI
httpx ← for API testing
pytest ← automated testing
textblob ← AI library for sentiment analysis
git add .# stage all files
git commit -m "your message"# save changes
git push # push to GitHub → triggers CI/CD
git pull # sync GitHub changes to your PC
git remote -v # check GitHub connection
git status # check current status
Run FastAPI Locally (without Docker)
pip install -r requirements.txt # install dependencies
uvicorn main:app --reload # start the app# Open → http://localhost:8000
Run Tests Locally
pytest # run all tests
pytest -v # run with details
Docker Commands
# Build Docker image
docker build --network=host -t docker-demo .# Run the container
docker run -p 8000:8000 docker-demo
# See running containers
docker ps
# Stop a container
docker stop <container_id>
Test Sentiment Analyzer in Browser
http://localhost:8000
http://localhost:8000/docs ← interactive API docs
http://localhost:8000/analyze?text=I love coding!
http://localhost:8000/analyze?text=This is awful!
http://localhost:8000/analyze?text=Today is okay
✅ How to Test the CI/CD Pipeline
Break it — see red ❌
# In test_main.py — change to wrong valuedeftest_positive_sentiment():
assertresponse.json()["sentiment"] =="negative"# wrong!