Docker Deployment
Übersicht
Solar-Log läuft als vollständiger Docker-Stack mit 4 Services:
- PostgreSQL - Datenbank (Port 5432)
- Backend - FastAPI (Port 8000)
- Frontend - React + Nginx (Port 3000)
- Nginx - Reverse Proxy (Port 8080)
Schnellstart
Voraussetzungen
Services starten
# Alle Services starten
docker compose up -d
# Status prüfen
docker compose ps
# Logs anzeigen
docker compose logs -f
Service URLs
| Service | URL | Beschreibung |
|---|---|---|
| Frontend | http://localhost:3000 | React Dashboard |
| Backend API | http://localhost:8000 | FastAPI Endpoints |
| API Docs | http://localhost:8000/docs | Swagger UI |
| Nginx Proxy | http://localhost:8080 | Reverse Proxy (empfohlen) |
Docker Images
Backend Image (471 MB)
Basierend auf python:3.11-slim:
- FastAPI + Uvicorn
- PostgreSQL Client
- pymodbus für Modbus TCP
- SQLAlchemy ORM
- Alembic Migrations
Health Check: curl http://localhost:8000/health
Frontend Image (86 MB)
Multi-Stage Build: 1. Builder: Node 20-alpine (npm build) 2. Runtime: Nginx alpine (static serving)
Health Check: wget localhost:80
Nginx Proxy
Konfiguration: - Port 8080 → Frontend (3000) - Port 8080/api → Backend (8000) - CORS Headers aktiviert - Gzip Compression
Konfiguration
Umgebungsvariablen
Erstelle .env Datei:
# Database
POSTGRES_USER=solarlog
POSTGRES_PASSWORD=solarlog_secret
POSTGRES_DB=solarlog
DATABASE_URL=postgresql://solarlog:solarlog_secret@postgres:5432/solarlog
# Backend
SECRET_KEY=your-secret-key-here
DEBUG=false
CORS_ORIGINS=http://localhost:3000,https://solarlog.karma.organic
# Polling
POLL_INTERVAL=60 # Sekunden
docker-compose.yml
services:
postgres:
image: postgres:16-alpine
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
volumes:
- postgres-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER}"]
interval: 10s
timeout: 5s
retries: 5
backend:
build: ./backend
environment:
DATABASE_URL: ${DATABASE_URL}
SECRET_KEY: ${SECRET_KEY}
depends_on:
postgres:
condition: service_healthy
ports:
- "8000:8000"
frontend:
build: ./frontend-web
ports:
- "3000:80"
nginx:
image: nginx:alpine
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
ports:
- "8080:80"
depends_on:
- backend
- frontend
Wartung
Services neu starten
Logs anzeigen
# Alle Services
docker compose logs -f
# Nur Backend
docker compose logs -f backend
# Letzte 50 Zeilen
docker compose logs --tail=50
Database Migrations
# Migration ausführen
docker compose exec backend alembic upgrade head
# Neue Migration erstellen
docker compose exec backend alembic revision --autogenerate -m "description"
# Migration History
docker compose exec backend alembic history
Datenbank Backup
# Backup erstellen
docker compose exec postgres pg_dump -U solarlog solarlog > backup.sql
# Backup wiederherstellen
cat backup.sql | docker compose exec -T postgres psql -U solarlog solarlog
Services stoppen
# Stoppen (Volumes behalten)
docker compose down
# Stoppen + Volumes löschen
docker compose down -v
# Alles aufräumen
docker compose down -v --rmi all
Troubleshooting
Backend startet nicht
# Logs prüfen
docker compose logs backend
# Database Connection testen
docker compose exec backend python -c "from app.database.session import engine; print(engine.connect())"
Frontend 404 Fehler
# Nginx Config prüfen
docker compose exec nginx cat /etc/nginx/nginx.conf
# Nginx neu laden
docker compose exec nginx nginx -s reload
Port bereits belegt
Disk Space voll
# Docker aufräumen
docker system prune -a --volumes
# Alte Images löschen
docker images | grep solarlog | awk '{print $3}' | xargs docker rmi
Performance Optimierung
Production Build
# Frontend Dockerfile
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
Resource Limits
services:
backend:
deploy:
resources:
limits:
cpus: '2'
memory: 512M
reservations:
cpus: '0.5'
memory: 256M
Health Checks
Alle Services haben automatische Health-Checks:
- PostgreSQL:
pg_isreadyalle 10s - Backend:
/healthEndpoint alle 30s - Frontend:
wget localhostalle 30s
Monitoring
Container Stats
# Real-time Stats
docker stats
# JSON Format
docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"