Installation Guide
This guide will help you install and run the E-commerce API on your local machine using Docker. The entire stack runs in containers, including the API server, PostgreSQL database, RabbitMQ message broker, and Celery workers.Prerequisites
Before you begin, ensure you have the following installed:- Docker (version 20.10 or higher)
- Docker Compose (version 1.29 or higher)
- Git (to clone the repository)
- At least 2GB of available RAM
- At least 5GB of available disk space
Docker Desktop for Mac and Windows includes Docker Compose by default. Linux users may need to install it separately.
Quick Start
Get up and running in under 5 minutes:Configure Environment Variables
Create a
.env file in the project root with the required configuration:Build Docker Images
Build the Docker images for all services:This process will:
- Build the API image based on the Dockerfile
- Install Python dependencies from requirements.txt
- Configure the application environment
The initial build may take 3-5 minutes as it downloads base images and installs all dependencies.
Start the Services
Launch all services using Docker Compose:This command starts four services:
- db - PostgreSQL 12 database
- api - Django REST API server (port 8000)
- rabbitmq - RabbitMQ message broker (ports 5672, 15672)
- celery - Celery worker for background tasks
Use
docker-compose up -d to run services in detached mode (background).Run Database Migrations
In a new terminal window, run the database migrations:This creates all necessary database tables and applies the schema.Expected output:
Create a Superuser (Optional)
Create an admin account to access the Django admin panel:Follow the prompts to set email and password.
Verify Installation
Verify the API is running by visiting:
- API Root: http://localhost:8000/
- Swagger Docs: http://localhost:8000/swagger/
- OpenAPI Schema: http://localhost:8000/api/v1/openapi/
- Admin Panel: http://localhost:8000/admin/
Docker Compose Architecture
The application uses four interconnected services defined in docker-compose.yml:Service Overview
Service Details
Database (db)
- Image: PostgreSQL 12 Alpine (lightweight)
- Purpose: Stores all application data (products, orders, users)
- Volume:
postgres_datafor persistent storage - Configuration: Uses environment variables from .env file (docker-compose.yml:9-13)
API Server (api)
- Image: Built from Dockerfile (custom ecommerce_api:v1)
- Purpose: Django REST Framework API server
- Port: 8000 (mapped to host)
- Server: Gunicorn WSGI server for production-grade performance (docker-compose.yml:20)
- Dependencies: Requires
dbservice to be running - Auto-restart: Restarts automatically on failure
RabbitMQ (rabbitmq)
- Image: RabbitMQ 3.10 with management plugin
- Purpose: Message broker for asynchronous task queuing
- Ports:
- 5672: AMQP protocol for Celery communication
- 15672: Web-based management UI
- Management UI: Access at http://localhost:15672 (guest/guest)
Celery Worker (celery)
- Image: Uses same image as API (ecommerce_api:v1)
- Purpose: Processes background tasks (order exports, email notifications)
- Features: Includes Celery Beat scheduler for periodic tasks (docker-compose.yml:39)
- Broker: Connects to RabbitMQ for task queue
Key Dependencies
The API relies on these core Python packages (from requirements.txt):Core Framework
- Django 5.0.6 - Web framework with ORM and admin interface
- djangorestframework 3.15.1 - REST API toolkit (requirements.txt:7)
- gunicorn 22.0.0 - Production WSGI server (requirements.txt:12)
- psycopg2 2.9.9 - PostgreSQL database adapter (requirements.txt:19)
Authentication & Security
- djangorestframework-simplejwt 5.3.1 - JWT authentication (requirements.txt:8)
- PyJWT 2.8.0 - JSON Web Token implementation (requirements.txt:21)
API Features
- django-filter 24.2 - Filtering support for querysets (requirements.txt:6)
- drf-yasg 1.21.7 - Swagger/OpenAPI documentation (requirements.txt:11)
- drf-spectacular 0.27.2 - Alternative OpenAPI schema generator (requirements.txt:9)
Payment Processing
- braintree 4.28.0 - Payment gateway integration (requirements.txt:1)
Background Tasks
- celery 5.4.0 - Distributed task queue (requirements.txt:2)
Developer Tools
- django-debug-toolbar 4.4.2 - Debugging toolbar for Django (requirements.txt:5)
- python-dotenv 1.0.1 - Environment variable management (requirements.txt:23)
Useful Commands
Starting and Stopping
Viewing Logs
Database Management
Django Management
Debugging
Configuration Details
Authentication Settings
JWT tokens are configured in config/settings.py:208-214:- Access Token Lifetime: 1 hour
- Refresh Token Lifetime: 3 days
- Token Type: Bearer
- Header Name: Authorization
API Settings
REST Framework configuration (config/settings.py:167-182):- Authentication: JWT-based (currently commented out for easier testing)
- Permissions: IsAuthenticated by default
- Pagination: Limit-offset with 10 items per page
- Filtering: Django-filter and search backends enabled
- Schema: drf-spectacular for OpenAPI generation
Database Configuration
The application supports both PostgreSQL and SQLite (config/settings.py:101-114):- PG_DB: PostgreSQL database for production use
- default: SQLite for development/testing
default alias in DATABASES.
Troubleshooting
Port 8000 already in use
Port 8000 already in use
Another application is using port 8000. Either:
- Stop the other application
- Change the port mapping in docker-compose.yml:
Database connection failed
Database connection failed
The API can’t connect to PostgreSQL. Check:
- Database service is running:
docker-compose ps db - Environment variables in .env match docker-compose.yml
- Wait a few seconds - PostgreSQL takes time to initialize
Build fails with dependency errors
Build fails with dependency errors
Python packages failed to install. Try:
-
Rebuild without cache:
- Check your internet connection
- Verify requirements.txt is not corrupted
Migrations not applying
Migrations not applying
Database schema updates failing. Common solutions:
-
Check database is running:
docker-compose ps db -
Reset database (WARNING: deletes all data):
-
Check for migration conflicts:
RabbitMQ connection refused
RabbitMQ connection refused
Celery can’t connect to RabbitMQ:
- Verify RabbitMQ is running:
docker-compose ps rabbitmq - Check RabbitMQ logs:
docker-compose logs rabbitmq - Wait 10-15 seconds after starting for RabbitMQ to fully initialize
- Restart Celery:
docker-compose restart celery
Permission denied errors
Permission denied errors
File permission issues in Docker:
-
On Linux, Docker may create files as root. Fix with:
- Check volume permissions in docker-compose.yml
Next Steps
Quickstart Tutorial
Make your first API call and get a working response
Interactive Documentation
Explore and test API endpoints in Swagger UI
Authentication Guide
Learn about JWT authentication and token management
API Reference
Detailed documentation for all endpoints