sveltekit-docker-nginx-auth Svelte Themes

Sveltekit Docker Nginx Auth

SvelteKit Docker + Nginx + Better Auth

A production-ready SvelteKit application with authentication using Better Auth, containerized with Docker, served by Nginx, and running with Bun.

Features

  • Authentication: Better Auth with email/password
  • Protected Routes: Server-side authentication middleware
  • Docker: Multi-stage build with optimized images
  • Nginx: Reverse proxy with load balancing
  • Bun: Fast JavaScript runtime
  • PostgreSQL: Database with health checks
  • Scaling: Multiple app replicas for high availability

Architecture

Internet → Nginx (Port 80) → Load Balancer → App Instances (x3) → PostgreSQL
  • Nginx: Reverse proxy, load balancer, serves static assets
  • App: 3 replicas of SvelteKit app running with Bun
  • PostgreSQL: Database with persistent storage

Prerequisites

  • Docker & Docker Compose
  • (Optional) Bun for local development

Quick Start

1. Clone and Setup

# Copy environment file
cp .env.example .env

# Edit .env and set your values
nano .env

2. Start Services

# Start all services
docker compose up -d

# View logs
docker compose logs -f

# Stop services
docker compose down

3. Access the Application

Configuration

Environment Variables

Edit .env file:

# Database
POSTGRES_USER=root
POSTGRES_PASSWORD=mysecretpassword
POSTGRES_DB=local

# Better Auth
BETTER_AUTH_SECRET=your-secret-key-change-in-production
BETTER_AUTH_URL=http://localhost

# External Port
PORT=80

Scaling

Adjust the number of app replicas in compose.yaml:

app:
  deploy:
    replicas: 3  # Change this number

Project Structure

.
├── Dockerfile              # Multi-stage Docker build
├── compose.yaml            # Docker Compose configuration
├── nginx/
│   └── nginx.conf          # Nginx configuration
├── src/
│   ├── hooks.server.ts     # Auth middleware
│   ├── lib/
│   │   ├── auth.ts         # Better Auth server config
│   │   └── auth-client.ts  # Better Auth client config
│   └── routes/
│       ├── +page.svelte    # Protected home page
│       ├── sign-in/        # Sign in page
│       └── sign-up/        # Sign up page
└── .env.example            # Environment variables template

Development

Local Development (without Docker)

# Start database only
docker compose up db -d

# Install dependencies
bun install

# Run dev server
bun run dev

Database Commands

# Push schema to database
bun run db:push

# Generate migrations
bun run db:generate

# Open Drizzle Studio
bun run db:studio

Production Deployment

1. Update Environment Variables

  • Set a strong BETTER_AUTH_SECRET
  • Update BETTER_AUTH_URL to your domain
  • Use strong database credentials

2. SSL/TLS Setup

Update nginx/nginx.conf to add SSL:

server {
    listen 443 ssl http2;
    ssl_certificate /etc/nginx/ssl/cert.pem;
    ssl_certificate_key /etc/nginx/ssl/key.pem;
    # ... rest of config
}

3. Deploy

# Build and start
docker compose up -d --build

# Check status
docker compose ps

# View logs
docker compose logs -f app

Troubleshooting

View Logs

# All services
docker compose logs -f

# Specific service
docker compose logs -f app
docker compose logs -f nginx
docker compose logs -f db

Restart Services

# Restart all
docker compose restart

# Restart specific service
docker compose restart app

Clean Start

# Stop and remove everything including volumes
docker compose down -v

# Rebuild and start
docker compose up -d --build

Check Container Status

docker compose ps

Security Considerations

  1. Change default credentials in production
  2. Use strong BETTER_AUTH_SECRET (generate with openssl rand -base64 32)
  3. Enable SSL/TLS with Let's Encrypt or similar
  4. Don't expose database port in production (remove ports from db service)
  5. Use secrets management for sensitive data
  6. Keep images updated regularly

Performance

  • 3 app replicas for load distribution
  • Nginx load balancing across instances
  • Bun runtime for fast execution
  • Connection pooling via PostgreSQL
  • Gzip compression enabled in Nginx

License

MIT

Top categories

Loading Svelte Themes