Fly.io
Learn how to deploy your application to Fly.io.
Fly.io is a platform for running full-stack apps and databases close to your users. It's great for Docker-based deployments and offers a generous free tier.
Why Fly.io?
- Global edge network - Deploy close to your users
- Docker-based - Full control over your container
- Free tier available - Great for getting started
- Simple scaling - Scale up or down easily
- Database support - Can provision PostgreSQL
Prerequisites
Before deploying to Fly.io, you need to:
- Set up Docker - Follow the Docker deployment guide to create a Dockerfile
- Install Fly CLI - Install the Fly CLI
Deploying to Fly.io
1. Create Fly.io Account
Sign up for a free account at fly.io.
2. Login to Fly CLI
fly auth login3. Launch Your App
From your project root, run:
fly launchThe CLI will:
- Detect your Dockerfile
- Ask for an app name
- Ask if you want to set up a PostgreSQL database
- Create a
fly.tomlconfiguration file
4. Configure fly.toml
The generated fly.toml should look like this:
app = "your-app-name"
primary_region = "iad"
[build]
[env]
PORT = "3000"
[http_service]
internal_port = 3000
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0
processes = ["app"]
[[vm]]
memory = "256mb"
cpu_kind = "shared"
cpus = 15. Set Environment Variables
Set your environment variables:
fly secrets set DATABASE_URL="postgresql://..."
fly secrets set BETTER_AUTH_SECRET="..."
fly secrets set NEXT_PUBLIC_SITE_URL="https://your-app.fly.dev"
fly secrets set STRIPE_SECRET_KEY="..."
fly secrets set RESEND_API_KEY="..."Or set multiple at once:
fly secrets set DATABASE_URL="..." BETTER_AUTH_SECRET="..." NEXT_PUBLIC_SITE_URL="..."6. Deploy
Deploy your application:
fly deployYour app will be available at https://your-app.fly.dev.
Database Migrations
Run migrations after deployment:
fly ssh console -C "npm run db:migrate"Or add to your Dockerfile's entrypoint script.
Provision Database
If you didn't provision a database during fly launch:
fly postgres create --name your-app-db
fly postgres attach your-app-dbThis will automatically set the DATABASE_URL secret.
Custom Domain
To use a custom domain:
-
Add your domain:
Terminalfly domains add your-domain.com -
Follow DNS configuration instructions
-
Update
NEXT_PUBLIC_SITE_URLsecret
Scaling
Scale your app:
# Scale to 2 instances
fly scale count 2
# Scale memory
fly scale vm shared-cpu-1x --memory 512Monitoring
View logs and metrics:
# View logs
fly logs
# View metrics
fly statusTroubleshooting
SSL Errors
If you encounter SSL errors, ensure your DATABASE_URL includes SSL parameters:
DATABASE_URL="postgresql://user:pass@host:5432/db?sslmode=require"Build Failures
- Check build logs:
fly logs - Verify Dockerfile is correct
- Ensure all dependencies are installed
- Make sure Prisma Client is generated
Database Connection
- Verify
DATABASE_URLsecret is set:fly secrets list - Check database is attached:
fly postgres list - Ensure database is in the same region