Skip to main content

Docker Deployment

Deploy SmarterAvatar using Docker for self-hosted environments.

Prerequisites

  • Docker and Docker Compose installed
  • PostgreSQL database
  • All API keys ready

Quick Start

1. Clone Repository

git clone https://github.com/fictiontribe/smarteravatar.git
cd smarteravatar

2. Configure Environment

cp .env.example .env
# Edit .env with your configuration

3. Build and Run

docker-compose up -d

The application will be available at http://localhost:3000.

Docker Compose Configuration

version: '3.8'

services:
app:
build:
context: .
dockerfile: Dockerfile
ports:
- "3000:3000"
environment:
- DATABASE_URL=${DATABASE_URL}
- GEMINI_API_KEY=${GEMINI_API_KEY}
- OPENAI_API_KEY=${OPENAI_API_KEY}
- LIVEAVATAR_API_KEY=${LIVEAVATAR_API_KEY}
depends_on:
- db
restart: unless-stopped

db:
image: postgres:16-alpine
environment:
POSTGRES_USER: smarteravatar
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: smarteravatar
volumes:
- postgres_data:/var/lib/postgresql/data
restart: unless-stopped

volumes:
postgres_data:

Dockerfile

The included Dockerfile uses multi-stage builds:

# Stage 1: Dependencies
FROM node:20-alpine AS deps
WORKDIR /app
COPY package.json pnpm-lock.yaml ./
RUN corepack enable pnpm && pnpm install --frozen-lockfile

# Stage 2: Builder
FROM node:20-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN corepack enable pnpm && pnpm build

# Stage 3: Runner
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV production

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static

USER nextjs
EXPOSE 3000
ENV PORT 3000

CMD ["node", "server.js"]

Database Migrations

Run migrations after the container starts:

# Run migrations
docker-compose exec app npx prisma migrate deploy

# Or as part of startup (add to entrypoint)
npx prisma migrate deploy && node server.js

Health Checks

Configure health checks in Docker Compose:

services:
app:
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/api/health"]
interval: 30s
timeout: 10s
retries: 3

Production Considerations

Environment Variables

Never commit .env files. Use:

  • Docker secrets
  • Environment variable injection
  • Cloud provider secret management

Scaling

For horizontal scaling:

  • Use external PostgreSQL
  • Configure shared session storage
  • Use a load balancer

Logging

Configure log aggregation:

services:
app:
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"

Troubleshooting

Container Won't Start

# Check logs
docker-compose logs app

# Check if port is available
lsof -i :3000

Database Connection Issues

# Test database connectivity
docker-compose exec app npx prisma db push --dry-run