This guide explains how to deploy the Flask Weather App to a VPS using Docker and Gunicorn for production use.
- Clean up unnecessary files and prepare for zipping:
# Remove any unnecessary files
rm -rf __pycache__
rm -rf .pytest_cache
rm -rf .venv
# Create the deployment package
tar -czvf flask_weather_app.tar.gz .- Transfer the zip file to your VPS:
scp flask_weather_app.tar.gz username@your-vps-ip:/path/to/deployment- Connect to your VPS:
ssh username@your-vps-ip- Navigate to your deployment directory and unzip:
cd /path/to/deployment
tar -xzf flask_weather_app.tar.gz
cd flask_weather_app- Build and deploy with Docker:
# Build the Docker image
docker build -t flask_app:0.01 .
# Deploy using Portainer
# Access your Portainer interface and:
# 1. Go to Stacks
# 2. Add a new stack
# 3. Upload or paste the docker-compose.yml content
# 4. Deploy the stackThis application uses Gunicorn as a production-grade WSGI server with the following features:
- Multiple worker processes (automatically scaled based on CPU cores)
- Production-grade HTTP server
- Error logging and monitoring
- Process management
Make sure to set these environment variables in Portainer:
OPENAI_MODEL_NAME: The OpenAI model to use (e.g., gpt-4)OPENAI_API_KEY: Your OpenAI API keyOPENCAGE_API_KEY: Your OpenCage API key
The Gunicorn configuration (gunicorn_config.py) is set up with:
- Automatic worker process scaling (CPU cores * 2 + 1)
- Connection backlog of 2048
- Worker timeout of 30 seconds
- Keep-alive connections
- Comprehensive logging
To adjust these settings, modify gunicorn_config.py and rebuild the Docker image.
After deployment:
- Check if the container is running:
docker ps- Check the container logs:
docker logs container_name- Test the API endpoint:
curl http://your-vps-ip:5005/api/coordinates- Start the application:
docker-compose up -d- Test the webhook endpoint:
curl -X POST http://localhost:5005/api/webhook/agent_zero \
-H "Content-Type: application/json" \
-d '[{
"body": {
"event": "messages.upsert",
"instance": "NOME_DA_SUA_INSTANCIA",
"data": {
"key": {
"remoteJid": "55<seu_numero_whatsapp_com_ddd>@s.whatsapp.net",
"fromMe": false,
"id": "3AB943940864A837CD4E"
},
"message": {
"conversation": "What is the weather in London?"
},
"messageType": "extendedTextMessage",
"messageTimestamp": 1727783931
}
}
}]'This simulates a WhatsApp message asking about the weather in London. The webhook endpoint will process this request and respond with weather information.
If you encounter issues:
- Check container logs:
docker logs container_name-
Verify environment variables are set correctly in Portainer
-
Ensure all ports are properly exposed and accessible
-
Check network configuration in docker-compose.yml matches your VPS network setup