Your project now supports Docker deployment for Railway and local development. This guide explains how to use Docker with your LegalContractAI backend.
- Docker Desktop installed (download)
- Docker Compose installed (included with Docker Desktop)
# Build the Docker image
docker build -t legalcontractai-backend ./backend
# Run the container
docker run -p 8000:8000 legalcontractai-backend# Build and start services
docker-compose up --build
# Run in background
docker-compose up -d --build
# View logs
docker-compose logs -f backend
# Stop services
docker-compose downCreate a .env file in the project root:
OPENAI_API_KEY=your_key_here
GEMINI_API_KEY=your_key_here
PINECONE_API_KEY=your_key_here
SUPABASE_URL=your_url_here
SUPABASE_KEY=your_key_hereThen run:
docker-compose up --buildRailway will automatically detect your Dockerfile in the backend directory. Simply:
- Push your code to GitHub
- Connect your GitHub repo to Railway
- Railway will detect the Dockerfile and build/deploy automatically
The railway.json file has been updated to explicitly use Docker:
{
"build": {
"builder": "DOCKER",
"dockerfile": "Dockerfile"
},
"deploy": {
"startCommand": "gunicorn app.main:app...",
"healthcheckPath": "/api/health"
}
}-
Push to GitHub
git add . git commit -m "Add Docker support" git push origin master
-
On Railway Dashboard
- Create a new project
- Connect your GitHub repository
- Select the backend directory as the root
- Railway will automatically detect Dockerfile and build it
- Add environment variables (OPENAI_API_KEY, GEMINI_API_KEY, etc.) in Railway dashboard
- Deploy!
-
Build & Deploy Status
- Railway will log the Docker build process
- Your app will be available at a Railway URL
- Monitor deployments in the Railway dashboard
- Multi-stage build: Reduces final image size by ~50%
- Python 3.11-slim: Lightweight base image (~150MB)
- Non-root user: Runs as
appuser(uid 1000) for security - Health checks: Automatic health monitoring
- Optimized dependencies: Only runtime dependencies in final image
- Install Python dependencies in builder stage
- Copy only compiled artifacts to runtime stage
- Create non-root user
- Start gunicorn with uvicorn workers
backend/
├── Dockerfile # Docker build configuration
├── .dockerignore # Excludes unnecessary files from Docker build
├── railway.json # Railway deployment configuration
├── requirements.txt # Python dependencies
├── app/
│ ├── main.py # FastAPI app entry point
│ ├── config.py # Configuration
│ ├── api/ # API routes
│ ├── agents/ # LLM agents
│ ├── services/ # Business logic
│ └── ...
# Build image
docker build -t legalcontractai-backend ./backend
# Run container
docker run -p 8000:8000 legalcontractai-backend
# View running containers
docker ps
# View logs
docker logs <container_id>
# Stop container
docker stop <container_id>
# Test with docker-compose
docker-compose up --build
docker-compose down
# Clean up
docker system prune -a- Check that all files in
.dockerignoreare correct - Ensure
requirements.txthas all dependencies - Check for missing or corrupted files
- Check logs:
docker logs <container_id> - Verify environment variables are set
- Ensure port 8000 is available
- Reduce worker count in gunicorn command
- Check for memory leaks in your code
- Monitor with
docker stats
- Docker layer caching will speed up subsequent builds
- Avoid frequent changes to
requirements.txt - Clean old images:
docker image prune
In your Railway dashboard, add these variables:
OPENAI_API_KEY=your_openai_key
GEMINI_API_KEY=your_gemini_key
PINECONE_API_KEY=your_pinecone_key
SUPABASE_URL=your_supabase_url
SUPABASE_KEY=your_supabase_key
DATABASE_URL=your_database_url (if needed)
After deployment, your API will be available at:
- Main API:
https://your-railway-url:8000 - Docs:
https://your-railway-url:8000/docs(Swagger UI) - ReDoc:
https://your-railway-url:8000/redoc - Health:
https://your-railway-url:8000/api/health
- Test locally with
docker-compose up - Push changes to GitHub
- Deploy to Railway
- Monitor logs and health checks
- Scale workers as needed based on traffic