Skip to main content
Deploy ChatJS using Docker for containerized, portable deployments.

Dockerfile

Create a Dockerfile in your project root:
FROM oven/bun:1 AS base

# Install dependencies
FROM base AS deps
WORKDIR /app
COPY package.json bun.lock ./
RUN bun install --frozen-lockfile

# Build
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

ENV NEXT_TELEMETRY_DISABLED=1
RUN bun run build

# Production
FROM base AS runner
WORKDIR /app

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1

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

COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"

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

Next.js Configuration

Enable standalone output in next.config.ts:
const nextConfig: NextConfig = {
  output: "standalone",
  // ... other config
};

Docker Compose

Create docker-compose.yml for local development with all dependencies:
services:
  app:
    build: .
    ports:
      - "3000:3000"
    environment:
      - DATABASE_URL=postgresql://postgres:postgres@db:5432/chatjs
      - REDIS_URL=redis://redis:6379
      - AUTH_SECRET=${AUTH_SECRET}
      - AI_GATEWAY_API_KEY=${AI_GATEWAY_API_KEY}
      # Optional - remove if attachments and imageGeneration are disabled in chat.config.ts
      - BLOB_READ_WRITE_TOKEN=${BLOB_READ_WRITE_TOKEN}
    depends_on:
      - db
      - redis

  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: chatjs
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data

volumes:
  postgres_data:
  redis_data:

Build and Run

# Build the image
docker build -t chatjs .

# Run with docker-compose
docker-compose up -d

# Run migrations
docker-compose exec app bun db:migrate

Environment Variables

Create a .env file for Docker:
# Required
AUTH_SECRET=your-secret-here
AI_GATEWAY_API_KEY=your-gateway-key

# Auth (choose one)
AUTH_GITHUB_ID=...
AUTH_GITHUB_SECRET=...

# Optional - for file attachments and image generation
BLOB_READ_WRITE_TOKEN=your-blob-token

# Optional
TAVILY_API_KEY=...

Production Considerations

Health Checks

Add a health check to your Dockerfile:
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD curl -f http://localhost:3000/api/health || exit 1

Managed Services

For production, consider using managed services instead of containerized databases:
  • Database: Neon, Supabase, or AWS RDS
  • Redis: Upstash, Redis Cloud, or AWS ElastiCache
  • Blob Storage: Vercel Blob, AWS S3, or Cloudflare R2

Container Registry

Push your image to a container registry:
# Tag and push to Docker Hub
docker tag chatjs your-username/chatjs:latest
docker push your-username/chatjs:latest

# Or use GitHub Container Registry
docker tag chatjs ghcr.io/your-org/chatjs:latest
docker push ghcr.io/your-org/chatjs:latest

Platform-Specific Guides