Overview
This guide will walk you through deploying your LangChat AI chatbot to production using:- Docker for containerization
- GitHub for version control and repository
- Google Cloud Run for serverless deployment
This guide is designed for both developers and non-developers. Follow each step carefully.
Prerequisites
Before starting, make sure you have:- ✅ Python 3.8+ installed
- ✅ Git installed
- ✅ Docker installed (optional, for local testing)
- ✅ GitHub account
- ✅ Google Cloud Platform (GCP) account
- ✅ LangChat project ready to deploy
Step 1: Prepare Your Project
Project Structure
Your project should have this structure:Create Your Application
Createmain.py:
Important: Use
host="0.0.0.0" and read port from environment variable PORT for Cloud Run compatibility.Create requirements.txt
Createrequirements.txt:
You can add other dependencies your project needs. Make sure to pin versions for production.
Create .env.example
Create.env.example (for reference, don’t commit actual .env):
Step 2: Create Dockerfile
CreateDockerfile:
Key Points:
- Use
python:3.10-slimfor smaller image size - Copy
requirements.txtfirst for Docker layer caching - Use
$PORTenvironment variable (Cloud Run sets this) - Use
0.0.0.0as host (required for Cloud Run)
Create .dockerignore
Create.dockerignore:
Step 3: Test Locally
Build Docker Image
Run Docker Container
Test Your Application
- Health Check: Visit
http://localhost:8000/health - Frontend: Visit
http://localhost:8000/frontend - API Test:
If everything works locally, you’re ready to deploy!
Step 4: Push to GitHub
Initialize Git Repository
Create GitHub Repository
- Go to GitHub
- Click New Repository
- Name it (e.g.,
langchat-chatbot) - Don’t initialize with README (you already have files)
- Click Create Repository
- Follow the commands shown (or use the commands above)
Verify Files in GitHub
Make sure these files are in your GitHub repository:- ✅
main.py - ✅
requirements.txt - ✅
Dockerfile - ✅
.dockerignore - ✅
.env.example(optional)
Never commit
.env file with actual API keys! Only commit .env.example.Step 5: Deploy to Google Cloud Run
Step 5.1: Set Up Google Cloud Project
- Go to Google Cloud Console
- Create a new project (or select existing)
- Enable Cloud Run API:
- Go to APIs & Services > Library
- Search for “Cloud Run API”
- Click Enable
Step 5.2: Connect GitHub Repository
- Go to Cloud Run in Google Cloud Console
- Click Create Service
- Select Deploy from source repository
- Click Set up with Cloud Build
- Connect Repository:
- Choose GitHub (Cloud Build GitHub App)
- Authorize GitHub access
- Select your repository
- Select branch (usually
main)
Step 5.3: Configure Build Settings
- Service Name: Enter a name (e.g.,
langchat-chatbot) - Region: Select your preferred region
- Authentication: Choose Allow unauthenticated invocations (or authenticated if needed)
- Container:
- Container port:
8000(or your configured port) - Dockerfile location:
Dockerfile(should auto-detect)
- Container port:
Step 5.4: Configure Service Settings
-
CPU:
- Allocated: Choose based on needs
- Minimum: 1 vCPU (recommended)
- Maximum: 4 vCPU (for scaling)
-
Memory:
- Minimum: 512 MiB
- Recommended: 1 GiB (for reranker models)
- Request timeout: 300 seconds (5 minutes)
- Maximum instances: Set based on your needs (default: 100)
- Minimum instances: 0 (to save costs) or 1 (for always-on)
Step 5.5: Set Environment Variables
In the Variables & Secrets section, add:Important: Make sure
PORT=8000 matches the port in your Dockerfile and code!Step 5.6: Deploy
- Click Create or Deploy
- Wait for build to complete (5-10 minutes)
- Once deployed, you’ll see:
- Service URL: Your API endpoint
- Status: Active
Step 5.7: Test Your Deployment
- Health Check: Visit
https://your-service-url.run.app/health - Frontend: Visit
https://your-service-url.run.app/frontend - API Test:
🎉 Your chatbot is now live in production!
Step 6: Update and Redeploy
Making Changes
- Update your code locally
- Test locally with Docker
- Commit and push to GitHub:
- Cloud Run automatically rebuilds and redeploys (if auto-deploy is enabled)
Manual Redeploy
If auto-deploy is disabled:- Go to Cloud Run in Google Cloud Console
- Select your service
- Click Edit & Deploy New Revision
- Click Deploy
Configuration Tips
Port Configuration
Make sure port is consistent:- Dockerfile:
EXPOSE 8000andENV PORT=8000 - main.py:
port = int(os.getenv("PORT", 8000)) - Cloud Run: Container port =
8000 - Environment:
PORT=8000
Resource Allocation
For Small Scale (testing, low traffic):- CPU: 1 vCPU
- Memory: 512 MiB
- Min instances: 0
- Max instances: 10
- CPU: 2 vCPU
- Memory: 1 GiB
- Min instances: 1
- Max instances: 100
- CPU: 4 vCPU
- Memory: 2 GiB
- Min instances: 2
- Max instances: 1000
Cost Optimization
- Set minimum instances to 0 (if acceptable cold start is OK)
- Use appropriate CPU/Memory (don’t over-provision)
- Set request timeout appropriately
- Monitor usage in Cloud Console
Troubleshooting
Issue: Build Fails
Solutions:- Check Dockerfile syntax
- Verify requirements.txt has correct packages
- Check Cloud Build logs in Google Cloud Console
Issue: Service Won’t Start
Solutions:- Verify PORT environment variable is set
- Check that host is
0.0.0.0in code - Review Cloud Run logs
Issue: Timeout Errors
Solutions:- Increase request timeout in Cloud Run settings
- Optimize your code for faster responses
- Check for slow API calls
Issue: Out of Memory
Solutions:- Increase memory allocation in Cloud Run
- Optimize reranker model usage
- Check for memory leaks
Issue: API Keys Not Working
Solutions:- Verify environment variables are set correctly
- Check for typos in variable names
- Ensure secrets are properly configured
Monitoring
View Logs
- Go to Cloud Run > Your Service
- Click Logs tab
- View real-time logs
Set Up Alerts
- Go to Monitoring > Alerting
- Create alert policies for:
- Error rate
- Latency
- Request count
Security Best Practices
- Never commit API keys to GitHub
- Use Cloud Run secrets for sensitive data
- Enable authentication if needed
- Use HTTPS (automatic with Cloud Run)
- Set up IAM roles properly
Next Steps
Now that your chatbot is deployed:- Monitor Performance - Optimize your deployment
- Customize Prompts - Improve responses
- Index Documents - Add knowledge base
- Scale Up - Handle more traffic
Summary
✅ Created Dockerfile for containerization✅ Pushed to GitHub for version control
✅ Deployed to Cloud Run for production
✅ Configured environment variables
✅ Tested deployment successfully
Your AI chatbot is now live and ready to use! 🚀
Questions? Check the Troubleshooting Guide or GitHub Issues!