gcp-hockey-results/motm_app/CONTAINER_DEPLOYMENT.md
2025-10-04 21:13:25 +08:00

189 lines
4.8 KiB
Markdown

# MOTM Application - Container Deployment
This document provides instructions for deploying the MOTM (Man of the Match) application using Docker containers.
## Quick Start
### Using Docker Compose (Recommended)
1. **Clone the repository and navigate to the project directory**
```bash
cd motm_app
```
2. **Start the application with PostgreSQL**
```bash
docker-compose up -d
```
3. **Access the application**
- Main page: http://localhost:5000
- Admin dashboard: http://localhost:5000/admin (username: `admin`, password: `letmein`)
### Using Docker Build
1. **Build the container**
```bash
docker build -f Containerfile -t motm-app .
```
2. **Run with external PostgreSQL**
```bash
docker run -d \
--name motm-app \
-p 5000:5000 \
-e DATABASE_TYPE=postgresql \
-e DB_HOST=your-postgres-host \
-e DB_PORT=5432 \
-e DB_NAME=motm_db \
-e DB_USER=motm_user \
-e DB_PASSWORD=motm_password \
motm-app
```
## Configuration
### Environment Variables
| Variable | Default | Description |
|----------|---------|-------------|
| `DATABASE_TYPE` | `postgresql` | Database type (postgresql/sqlite) |
| `DB_HOST` | `postgres` | Database host (use `postgres` for docker-compose) |
| `DB_PORT` | `5432` | Database port |
| `DB_NAME` | `motm_db` | Database name |
| `DB_USER` | `motm_user` | Database username |
| `DB_PASSWORD` | `motm_password` | Database password |
| `FLASK_ENV` | `production` | Flask environment |
| `FLASK_RUN_HOST` | `0.0.0.0` | Flask host |
| `FLASK_RUN_PORT` | `5000` | Flask port |
| `SECRET_KEY` | `your-secret-key-change-this-in-production` | Flask secret key |
### Production Security
**Important**: Before deploying to production, change the following:
1. **Database credentials** in `docker-compose.yml`
2. **Secret key** in environment variables
3. **Admin password** (use the admin profile page after first login)
## Container Features
### Multi-stage Build
- **Builder stage**: Installs dependencies and builds Python packages
- **Runtime stage**: Minimal image with only runtime dependencies
### Security
- Runs as non-root user (`appuser`)
- Minimal attack surface with slim base image
- Health checks for container monitoring
### Database Integration
- Automatic database connection waiting
- Database initialization support
- PostgreSQL optimized configuration
## Monitoring
### Health Checks
- Application health check: `curl -f http://localhost:5000/`
- Database health check: `pg_isready`
### Logs
```bash
# View application logs
docker-compose logs motm-app
# View database logs
docker-compose logs postgres
# Follow logs in real-time
docker-compose logs -f motm-app
```
## Maintenance
### Backup Database
```bash
# Create backup
docker exec motm-postgres pg_dump -U motm_user motm_db > backup.sql
# Restore backup
docker exec -i motm-postgres psql -U motm_user motm_db < backup.sql
```
### Update Application
```bash
# Pull latest changes
git pull
# Rebuild and restart
docker-compose down
docker-compose up -d --build
```
### Reset Database
```bash
# Stop services
docker-compose down
# Remove database volume
docker volume rm motm_app_postgres_data
# Start fresh
docker-compose up -d
```
## Troubleshooting
### Common Issues
1. **Database connection errors**
- Check if PostgreSQL container is running: `docker-compose ps`
- Verify database credentials in environment variables
- Check database logs: `docker-compose logs postgres`
2. **Application won't start**
- Check application logs: `docker-compose logs motm-app`
- Verify all environment variables are set correctly
- Ensure database is healthy before application starts
3. **Port conflicts**
- Change port mapping in `docker-compose.yml`
- Example: `"8080:5000"` to use port 8080 instead of 5000
### Debug Mode
```bash
# Run container in interactive mode for debugging
docker run -it --rm \
-p 5000:5000 \
-e DATABASE_TYPE=postgresql \
-e DB_HOST=your-postgres-host \
-e DB_PORT=5432 \
-e DB_NAME=motm_db \
-e DB_USER=motm_user \
-e DB_PASSWORD=motm_password \
motm-app /bin/bash
```
## File Structure
```
motm_app/
├── Containerfile # Docker container definition
├── docker-compose.yml # Multi-service orchestration
├── .dockerignore # Files to exclude from build
├── init.sql # Database initialization script
├── requirements.txt # Python dependencies
├── main.py # Main application file
├── static/ # Static assets (CSS, JS, images)
├── templates/ # HTML templates
└── data/ # Persistent data directory
```
## Support
For issues or questions about container deployment, please check:
1. Application logs: `docker-compose logs motm-app`
2. Database logs: `docker-compose logs postgres`
3. Container status: `docker-compose ps`