qrmaster/docker/README.md

277 lines
5.3 KiB
Markdown

# Docker Setup for QR Master
This directory contains Docker configuration files for running QR Master with PostgreSQL database.
## 🚀 Quick Start
### Development (Database Only)
For local development where you run Next.js on your host machine:
```bash
# Start PostgreSQL and Redis
docker-compose -f docker-compose.dev.yml up -d
# Run database migrations
npm run db:migrate
# Start the development server
npm run dev
```
Access:
- **Application**: http://localhost:3050
- **Database**: localhost:5432
- **Redis**: localhost:6379
- **Adminer (DB UI)**: http://localhost:8080
### Production (Full Stack)
To run the entire application in Docker:
```bash
# Build and start all services
docker-compose up -d --build
# Run database migrations
docker-compose exec web npx prisma migrate deploy
# (Optional) Seed the database
docker-compose exec web npm run db:seed
```
Access:
- **Application**: http://localhost:3050
- **Database**: localhost:5432
- **Redis**: localhost:6379
- **Adminer (DB UI)**: http://localhost:8080 (only with --profile dev)
To include Adminer in production mode:
```bash
docker-compose --profile dev up -d
```
## 📦 Services
### PostgreSQL (db)
- **Image**: postgres:16-alpine
- **Port**: 5432
- **Database**: qrmaster
- **User**: postgres
- **Password**: postgres (change in production!)
### Redis (redis)
- **Image**: redis:7-alpine
- **Port**: 6379
- **Max Memory**: 256MB with LRU eviction policy
- **Persistence**: AOF enabled
### Next.js Application (web)
- **Port**: 3050
- **Environment**: Production
- **Health Check**: HTTP GET on localhost:3050
### Adminer (adminer)
- **Image**: adminer:latest
- **Port**: 8080
- **Purpose**: Database management UI
- **Profile**: dev (optional in production)
## 🗄️ Database Management
### Migrations
```bash
# Create a new migration
npm run db:migrate
# Deploy migrations in Docker
docker-compose exec web npx prisma migrate deploy
# Reset database (caution!)
docker-compose exec web npx prisma migrate reset
```
### Prisma Studio
```bash
# On host machine
npm run db:studio
# Or in Docker
docker-compose exec web npx prisma studio
```
### Backup and Restore
```bash
# Backup
docker-compose exec db pg_dump -U postgres qrmaster > backup.sql
# Restore
docker-compose exec -T db psql -U postgres qrmaster < backup.sql
```
## 🔧 Useful Commands
### View Logs
```bash
# All services
docker-compose logs -f
# Specific service
docker-compose logs -f web
docker-compose logs -f db
docker-compose logs -f redis
```
### Shell Access
```bash
# Next.js container
docker-compose exec web sh
# PostgreSQL container
docker-compose exec db psql -U postgres -d qrmaster
# Redis container
docker-compose exec redis redis-cli
```
### Stop and Clean
```bash
# Stop all services
docker-compose down
# Stop and remove volumes (deletes data!)
docker-compose down -v
# Stop and remove everything including images
docker-compose down -v --rmi all
```
## 🔐 Environment Variables
Create a `.env` file in the root directory (copy from `env.example`):
```bash
cp env.example .env
```
Required variables:
- `DATABASE_URL`: PostgreSQL connection string
- `NEXTAUTH_SECRET`: Secret for NextAuth.js
- `NEXTAUTH_URL`: Application URL
- `IP_SALT`: Salt for hashing IP addresses
- `REDIS_URL`: Redis connection string
## 🌐 Network Architecture
All services run on a custom bridge network `qrmaster-network` which allows:
- Service discovery by container name
- Network isolation from other Docker projects
- Internal DNS resolution
## 📊 Volumes
### Persistent Data
- `dbdata`: PostgreSQL data
- `redisdata`: Redis data
### Volume Management
```bash
# List volumes
docker volume ls
# Inspect volume
docker volume inspect qrmaster_dbdata
# Remove all unused volumes
docker volume prune
```
## 🐛 Troubleshooting
### Database Connection Issues
```bash
# Check if database is ready
docker-compose exec db pg_isready -U postgres
# Check database logs
docker-compose logs db
# Restart database
docker-compose restart db
```
### Application Won't Start
```bash
# Check health status
docker-compose ps
# View application logs
docker-compose logs web
# Rebuild the application
docker-compose up -d --build web
```
### Port Already in Use
If ports 3050, 5432, 6379, or 8080 are already in use:
```bash
# Find process using port
# Windows
netstat -ano | findstr :3050
# Linux/Mac
lsof -i :3050
# Kill process or change port in docker-compose.yml
```
## 🔄 Updates and Maintenance
### Update Dependencies
```bash
# Update Node packages
npm update
# Rebuild Docker images
docker-compose build --no-cache
```
### Update Docker Images
```bash
# Pull latest images
docker-compose pull
# Restart with new images
docker-compose up -d
```
## 📝 Notes
- **Development**: Use `docker-compose.dev.yml` to run only the database and Redis
- **Production**: Use `docker-compose.yml` to run the full stack
- **Security**: Always change default passwords in production
- **Backups**: Implement regular database backups in production
- **Scaling**: For production, consider using PostgreSQL replication and Redis Sentinel
## 🆘 Support
For more information, see:
- [Docker Documentation](https://docs.docker.com/)
- [PostgreSQL Documentation](https://www.postgresql.org/docs/)
- [Prisma Documentation](https://www.prisma.io/docs/)
- [Next.js Documentation](https://nextjs.org/docs)