General
Deployment

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:

  1. Set up Docker - Follow the Docker deployment guide to create a Dockerfile
  2. 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

Terminal
fly auth login

3. Launch Your App

From your project root, run:

Terminal
fly launch

The CLI will:

  • Detect your Dockerfile
  • Ask for an app name
  • Ask if you want to set up a PostgreSQL database
  • Create a fly.toml configuration file

4. Configure fly.toml

The generated fly.toml should look like this:

fly.toml
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 = 1

5. Set Environment Variables

Set your environment variables:

Terminal
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:

Terminal
fly secrets set DATABASE_URL="..." BETTER_AUTH_SECRET="..." NEXT_PUBLIC_SITE_URL="..."

6. Deploy

Deploy your application:

Terminal
fly deploy

Your app will be available at https://your-app.fly.dev.

Database Migrations

Run migrations after deployment:

Terminal
fly ssh console -C "npm run db:migrate"

Or use db:push for development:

Terminal
fly ssh console -C "npm run db:push"

Or add to your Dockerfile's entrypoint script.

Provision Database

If you didn't provision a database during fly launch:

Terminal
fly postgres create --name your-app-db
fly postgres attach your-app-db

This will automatically set the DATABASE_URL secret.

Custom Domain

To use a custom domain:

  1. Add your domain:

    Terminal
    fly domains add your-domain.com
  2. Follow DNS configuration instructions

  3. Update NEXT_PUBLIC_SITE_URL secret

Scaling

Scale your app:

Terminal
# Scale to 2 instances
fly scale count 2

# Scale memory
fly scale vm shared-cpu-1x --memory 512

Monitoring

View logs and metrics:

Terminal
# View logs
fly logs

# View metrics
fly status

Troubleshooting

SSL Errors

If you encounter SSL errors, ensure your DATABASE_URL includes SSL parameters:

.env
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

Database Connection

  • Verify DATABASE_URL secret is set: fly secrets list
  • Check database is attached: fly postgres list
  • Ensure database is in the same region