DevOps in Django E-Commerce System with DRF and Docker
⚙️ Introduction
When your Django + DRF backend powers a mobile e-commerce app, every release matters.
A single downtime can stop users from logging in, checking out, or receiving push notifications.
To keep development fast and reliable, you need DevOps — an approach combining development and operations to automate everything from testing to deployment.
This guide shows how to apply DevOps to a Python Django + DRF + PostgreSQL + Docker e-commerce backend that serves mobile users.
🧱 System Overview
Architecture Components:
- Backend: Django + Django REST Framework (API for mobile app)
- Database: PostgreSQL
- Containerization: Docker + Docker Compose
- Deployment: GitHub Actions → AWS EC2 / DigitalOcean droplet
- Monitoring: Prometheus + Grafana
Goal:
Deliver continuous integration, testing, and deployment for a stable, API-driven e-commerce backend.
🔄 DevOps Workflow Overview
flowchart TD
A["Developer Commit (GitHub)"] --> B["CI Pipeline (GitHub Actions)"]
B --> C["Build Docker Image"]
C --> D["Run Unit & API Tests (pytest + DRF client)"]
D --> E["Push to Registry (Docker Hub / ECR)"]
E --> F["Deploy via Docker Compose / K8s"]
F --> G["Monitor with Prometheus + Grafana"]
🧩 1. Continuous Integration (CI)
Each commit triggers automatic build and test processes to ensure API stability.
Example: .github/workflows/ci.yml
name: Django CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:14
env:
POSTGRES_DB: ecommerce
POSTGRES_USER: django
POSTGRES_PASSWORD: password
ports: ['5432:5432']
env:
DATABASE_URL: postgres://django:password@localhost:5432/ecommerce
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
pip install -r requirements.txt
- name: Run Tests
run: pytest -v
✅ Result: Only tested code gets merged, reducing runtime errors for mobile users.
🚀 2. Continuous Delivery (CD)
When CI passes, the code is automatically packaged and deployed via Docker.
Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["gunicorn", "core.wsgi:application", "--bind", "0.0.0.0:8000"]
docker-compose.yml
version: '3'
services:
web:
build: .
container_name: ecommerce_api
command: gunicorn core.wsgi:application --bind 0.0.0.0:8000
ports:
- "8000:8000"
depends_on:
- db
db:
image: postgres:14
environment:
POSTGRES_DB: ecommerce
POSTGRES_USER: django
POSTGRES_PASSWORD: password
🟢 Goal: CI → Docker build → Push to registry → Auto deploy to production (e.g., via GitHub Actions + SSH to EC2).
🧪 3. Automated Testing
Use pytest and DRF’s API client for unit and endpoint testing.
from rest_framework.test import APIClient
def test_product_list(api_client):
client = APIClient()
response = client.get("/api/products/")
assert response.status_code == 200
assert isinstance(response.json(), list)
🧠 Tip: Run all API tests before deployment to prevent broken endpoints in your mobile app.
🧰 4. Infrastructure as Code (IaC)
Use Terraform or Ansible to provision servers, networks, and databases automatically.
Terraform Example:
resource "aws_instance" "backend" {
ami = "ami-0f3e2d1e1d33b3b57"
instance_type = "t3.medium"
tags = { Name = "django-ecommerce" }
}
Reproducible infrastructure ensures new environments (e.g., staging, production) can be deployed identically.
📊 5. Monitoring & Logging
Integrate Prometheus + Grafana for performance metrics and alerts.
You can monitor:
- API latency
- Response time
- Database connections
- CPU/memory usage
Example alert:
“API latency above 2s for /checkout — possible DB bottleneck.”
Logs can be shipped to ELK Stack (Elasticsearch + Logstash + Kibana) for deeper analysis.
🧠 6. Security & Environment Management
- Store secrets in GitHub Secrets or AWS SSM Parameter Store.
- Use
.envfiles in Docker (not committed to Git). - Rotate keys and restrict SSH access.
- Run container security scans with Trivy.
Example .env:
DEBUG=False
DATABASE_URL=postgres://django:password@db:5432/ecommerce
SECRET_KEY=change_this_in_production
📱 7. Serving the Mobile App
Your DRF API serves as the mobile app backend.
Each build deploys:
- Updated endpoints for products, cart, checkout
- JWT or OAuth2 authentication
- Versioned APIs (e.g.,
/api/v1/,/api/v2/)
CI/CD ensures:
New app versions can be released without breaking the existing mobile client.
🌐 System Architecture
graph TD
APP["Mobile App"] --> API["Django REST Framework API"]
API --> DB["PostgreSQL"]
API --> CACHE["Redis / Celery Worker"]
API --> MON["Prometheus + Grafana"]
DEV["CI/CD Pipeline (GitHub Actions)"] --> API
API --> DOCKER["Docker Container on EC2"]
🧭 Benefits of DevOps for Django + DRF Project
| Benefit | Description |
|---|---|
| 🚀 Faster Delivery | Automatic deployment reduces release time |
| ✅ Higher Quality | Continuous testing prevents API errors |
| 🔒 Secure | Controlled secrets and containers |
| 📈 Scalable | Infrastructure as Code makes scaling easy |
| 🔍 Transparent | Monitoring keeps team informed in real time |
Get in Touch with us
Related Posts
- 基于启发式与新闻情绪的短期价格方向评估(Python)
- Estimating Short-Term Price Direction with Heuristics and News Sentiment (Python)
- Rust vs Python:AI 与大型系统时代的编程语言选择
- Rust vs Python: Choosing the Right Tool in the AI & Systems Era
- How Software Technology Can Help Chanthaburi Farmers Regain Control of Fruit Prices
- AI 如何帮助发现金融机会
- How AI Helps Predict Financial Opportunities
- 在 React Native 与移动应用中使用 ONNX 模型的方法
- How to Use an ONNX Model in React Native (and Other Mobile App Frameworks)
- 叶片病害检测算法如何工作:从相机到决策
- How Leaf Disease Detection Algorithms Work: From Camera to Decision
- Smart Farming Lite:不依赖传感器的实用型数字农业
- Smart Farming Lite: Practical Digital Agriculture Without Sensors
- 为什么定制化MES更适合中国工厂
- Why Custom-Made MES Wins Where Ready-Made Systems Fail
- How to Build a Thailand-Specific Election Simulation
- When AI Replaces Search: How Content Creators Survive (and Win)
- 面向中国市场的再生资源金属价格预测(不投机、重决策)
- How to Predict Metal Prices for Recycling Businesses (Without Becoming a Trader)
- Smart Durian Farming with Minimum Cost (Thailand)













