Deploy API to Fly.io

Fly.io supports Docker deployment with built-in HTTPS, health checks, and automatic deployments.

Prerequisites

1. Create App

fly apps create your-app-name

2. Create fly.toml

Create fly.toml in the project root:

app = 'your-app-name'
primary_region = 'iad'

[build]
  dockerfile = 'api/Dockerfile'

[deploy]
  release_command = 'npm run db:migrate'

[http_service]
  internal_port = 16888
  force_https = true
  auto_stop_machines = 'off'
  auto_start_machines = true
  min_machines_running = 1

[[http_service.checks]]
  interval = "10s"
  timeout = "5s"
  grace_period = "30s"
  method = "GET"
  path = "/v1/health"

[[vm]]
  size = 'shared-cpu-1x'
  memory = '1gb'

Configuration

ParameterDescription
primary_regionDeploy region. iad US East, nrt Tokyo
release_commandRuns database migration before deployment
internal_portApp listening port
auto_stop_machinesSet to off to keep running
min_machines_runningMinimum running machines

3. Set Environment Variables

fly secrets set \
  "NODE_ENV=production" \
  "JWT_SECRET=your-jwt-secret" \
  ...
  -a your-app-name

See Environment Variables for the full list.

4. Deploy

fly deploy

Fly.io will automatically:

  1. Build the Docker image
  2. Run npm run db:migrate (deployment continues only if migration succeeds)
  3. Start the new container
  4. Switch traffic after health check passes

5. Custom Domain

fly certs add api.yourdomain.com -a your-app-name

Add a CNAME record in your DNS:

api.yourdomain.com → your-app-name.fly.dev

Useful Commands

# View logs
fly logs -a your-app-name

# List machines
fly machine list -a your-app-name

# Update secrets
fly secrets set KEY=VALUE -a your-app-name

# Redeploy
fly deploy

# SSH into container
fly ssh console -a your-app-name

Notes