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 .env files 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

Chat with Us on LINE

iiitum1984

Speak to Us or Whatsapp

(+66) 83001 0222

Related Posts

Our Products