This project demonstrates a Django REST API implementation with Docker, showcasing best practices for containerization and deployment configuration.
tasks_api/
├── api/ # API application
│ ├── views.py # API endpoints
│ ├── models.py # Data models
│ ├── serializers.py # Data serializers
│ └── urls.py # API routing
├── backend/ # Django project settings
│ ├── settings.py # Project configuration
│ ├── urls.py # Project URL routing
│ └── wsgi.py # WSGI configuration
├── deploy/ # Deployment configurations
│ ├── postgres/ # PostgreSQL service
│ │ ├── Dockerfile # PostgreSQL container configuration
│ │ └── init.sql # Database initialization script
│ └── server/ # API server service
│ └── Dockerfile # Server container configuration
├── docker-compose.yaml # Docker services orchestration
├── Makefile # Development automation
├── .env # Environment variables (not in version control)
├── .env.example # Environment variables template
├── requirements.txt # Python dependencies
└── manage.py # Django management script
- RESTful API endpoints for task management
- Django REST Framework with browsable API interface
- PostgreSQL Database for data persistence
- Docker containerization with multi-stage builds
- Health checks and container orchestration
- Environment-based configuration
- Comprehensive API documentation
- Test coverage for API endpoints
- Python 3.12+
- PostgreSQL 15+
- Docker 24.0+ and Docker Compose V2
- Make (optional, for using Makefile commands)
-
Clone the repository:
git clone https://github.com/oragazzo/tasks_api.git cd tasks_api
-
Set up environment variables:
cp .env.example .env # Edit .env file with your desired configuration
-
Start the application using Docker Compose:
docker-compose up --build
-
Create a virtual environment:
# Option 1: Using Conda (Recommended) conda create -n tasks_api python=3.12 conda activate tasks_api # Option 2: Using venv python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Set up environment variables:
cp .env.example .env # Edit .env file with your configuration # For local development, make sure DATABASE_HOST=localhost
-
Install dependencies:
pip install -r requirements.txt
-
Create PostgreSQL database:
createdb basic_api
-
Run migrations and create superuser:
python manage.py migrate python manage.py createsuperuser
-
Start the development server:
python manage.py runserver
The project uses a .env
file for configuration. Copy .env.example
to .env
and adjust the values:
# Django Settings
DEBUG=1
SECRET_KEY=your-secret-key-here
ALLOWED_HOSTS=localhost,127.0.0.1
# Database Settings
DATABASE_NAME=basic_api
DATABASE_USER=<DB_USER>
DATABASE_PASSWORD=<DB_PASSWORD>
DATABASE_HOST=db # Use 'db' for Docker, 'localhost' for local development
DATABASE_PORT=5432
# PostgreSQL Container Settings
POSTGRES_DB=basic_api
POSTGRES_USER=<PG_USER>
POSTGRES_PASSWORD=<PG_PASSWORD>
- The Docker setup includes hot-reload for development
- Database data is persisted in a Docker volume
- PostgreSQL is accessible on port 5432
- The API server runs on port 8000
- Environment variables are loaded from
.env
file
Common commands for managing the Docker environment:
# Start services in development mode
docker-compose up
# Start services in detached mode
docker-compose up -d
# Rebuild and start services
docker-compose up --build
# Stop services
docker-compose down
# Stop services and remove volumes
docker-compose down -v
# View logs
docker-compose logs -f
# View logs for specific service
docker-compose logs -f api
- The
.env
file contains sensitive information and is not included in version control - Default Django admin credentials should be changed in production
- Debug mode should be disabled in production
- Use strong passwords for database and Django admin
- Regular security updates should be applied to all dependencies
This project is licensed under the MIT License - see the LICENSE file for details