Overview
Docker provides a consistent, portable deployment environment for SKU Semantic Search. The included Docker Compose configuration sets up both the FastAPI application and PostgreSQL database with a single command.Prerequisites
- Docker: Version 20.10 or higher
- Docker Compose: Version 2.0 or higher
- Linux
- macOS
- Windows
Docker Compose configuration
The project includes a completedocker-compose.yml file:
Configuration breakdown
API service
API service
Build context:
. (current directory)- Uses the included
Dockerfileto build the application image
8000:8000- Exposes FastAPI on port 8000 of the host machine
.:/app- Syncs local code changes into the container
- Enables live reloading during development
DATABASE_URL: Usesdbas hostname (Docker Compose service name)GEMINI_API_KEY: Loaded from host.envfileJWT_SECRET: Hardcoded for demo (use secrets in production)
depends_onwithcondition: service_healthyensures PostgreSQL is ready before starting the API
--reloadenables auto-restart on code changes (remove in production)
Database service
Database service
Image:
pgvector/pgvector:pg16- Pre-built image with PostgreSQL 16 and pgvector extension
5435:5432- External port 5435 avoids conflicts with local PostgreSQL
- Internal port 5432 is standard PostgreSQL
POSTGRES_USER: Superuser namePOSTGRES_PASSWORD: Superuser password (change in production!)POSTGRES_DB: Default database to create
postgres_data- Named volume persists data across container restarts
- Stored in Docker’s volume directory
- Runs
pg_isreadyevery 10 seconds - API service waits until this returns healthy
Volumes
Volumes
postgres_data:
- Named volume for database persistence
- Survives
docker-compose down - Deleted only with
docker-compose down -v
Dockerfile
The application uses a multi-stage Dockerfile for efficient builds (Dockerfile:1):
- Slim base image:
python:3.11-slimreduces image size - Layer caching: Copying
requirements.txtfirst allows Docker to cache dependencies - No cache:
--no-cache-dirreduces image size by not storing pip cache - System dependencies:
gccandlibpq-devare needed to compilepsycopg2-binary
Deployment steps
Build and start services
- Builds the API image from the Dockerfile
- Pulls the pgvector PostgreSQL image
- Creates a network for service communication
- Starts both containers in detached mode
Seed the database
Access the API
The API is now available at:
- Root: http://localhost:8000
- Docs: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
Docker Compose commands
Production deployment
For production environments, modify the configuration:Production docker-compose.yml
restart: always- Containers restart on failure--workers 4- Multiple Uvicorn workers for concurrency- Removed
--reload- No auto-restart on file changes - Removed code volume mount - Bakes code into image
- All secrets loaded from environment (not hardcoded)
- Backup volume for database dumps
Production Dockerfile
- Smaller final image (build tools not included)
- Faster deployments
- Better security (no compilers in production)
Reverse proxy with Nginx
For production, place Nginx in front of the API:Monitoring and logging
View container metrics
Centralized logging
Configure Docker to use a logging driver:- ELK Stack: Elasticsearch, Logstash, Kibana
- Loki: Grafana Loki for log aggregation
- CloudWatch: AWS CloudWatch Logs
Troubleshooting
Port already in use
Port already in use
Error:
Bind for 0.0.0.0:8000 failed: port is already allocatedSolution:Database connection fails
Database connection fails
Error:
psycopg2.OperationalError: could not connect to serverSolution:- Check database is healthy:
docker-compose ps - View database logs:
docker-compose logs db - Verify
DATABASE_URLusesdbas hostname (notlocalhost) - Wait for health check: API starts only when DB is ready
Image build fails
Image build fails
Error: During
docker-compose up --buildSolution:- Check Dockerfile syntax
- Ensure requirements.txt exists and is valid
- Clear build cache:
docker-compose build --no-cache - Check disk space:
docker system df
Changes not reflected
Changes not reflected
Symptoms: Code changes don’t appear in running containerSolution:
- Verify volume mount exists:
- .:/app - Check
--reloadflag is present in command - For Dockerfile changes, rebuild:
docker-compose up --build - Restart services:
docker-compose restart api
Next steps
Environment setup
Configure environment variables for Docker
Database setup
Optimize PostgreSQL for production
Architecture overview
Understand the deployed system architecture
Quickstart
Test the deployed API