9498aa82b4
Entire-Checkpoint: 3684d8febba1
3.3 KiB
3.3 KiB
Deploying prompts.chat to Cloudflare Containers
This guide covers deploying the Next.js application to Cloudflare Containers.
Prerequisites
- Docker running locally
- Wrangler CLI (
npm i -g wrangler) - A Cloudflare account on the Workers Paid plan
- An external PostgreSQL database (e.g. Neon, Supabase, or self-hosted)
Setup
1. Install dependencies
npm install @cloudflare/containers
2. Configure secrets
Set required environment variables as Wrangler secrets. These are injected into the container at runtime:
# Required
wrangler secret put DATABASE_URL
wrangler secret put AUTH_SECRET
# OAuth (if using these providers)
wrangler secret put AUTH_GITHUB_ID
wrangler secret put AUTH_GITHUB_SECRET
wrangler secret put AUTH_GOOGLE_ID
wrangler secret put AUTH_GOOGLE_SECRET
# Optional
wrangler secret put OPENAI_API_KEY
wrangler secret put SENTRY_DSN
3. Deploy
npx wrangler deploy
On the first deploy, Wrangler will:
- Build the Docker image locally using
Dockerfile.cloudflare - Push the image to Cloudflare's managed registry
- Deploy the Worker that routes traffic to the container
The first deploy takes a few minutes for image push and container provisioning. Subsequent deploys are faster due to layer caching.
4. Check status
npx wrangler containers list
Architecture
User Request → Cloudflare Network → Worker (router) → Container (Next.js on Node.js)
↓
PostgreSQL DB
- Worker (
cloudflare/worker.js): Thin routing layer that forwards all requests to the container - Container (
Dockerfile.cloudflare): Multi-stage Node.js image running the Next.js standalone server - The container runs the full Node.js runtime, so all dependencies (Prisma, next-auth, next-intl, sharp) work without modification
Configuration
Key settings in wrangler.jsonc:
| Setting | Default | Description |
|---|---|---|
max_instances |
3 | Maximum concurrent container instances |
instance_type |
standard-1 | 2 vCPU, 2 GiB memory |
sleepAfter |
5m | Container sleeps after 5 minutes of inactivity |
Adjust max_instances and instance_type based on your traffic. See instance types for all options.
Local development
Local development remains unchanged:
npm run dev
To test the Docker image locally:
docker build -f Dockerfile.cloudflare -t prompts-chat .
docker run -p 3000:3000 --env-file .env prompts-chat
Notes
- Cold starts: Containers take 2-3 seconds to wake from sleep. The
sleepAftersetting incloudflare/worker.jscontrols when idle containers go to sleep. - Database: Use an external PostgreSQL provider. Cloudflare Hyperdrive can be added for edge connection pooling if needed.
- Containers Beta: Cloudflare Containers is currently in beta. See beta info for limitations.