Deploy API to Fly.io
Fly.io supports Docker deployment with built-in HTTPS, health checks, and automatic deployments.
Prerequisites
- Install flyctl
- Login:
fly auth login
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
| Parameter | Description |
|---|---|
primary_region | Deploy region. iad US East, nrt Tokyo |
release_command | Runs database migration before deployment |
internal_port | App listening port |
auto_stop_machines | Set to off to keep running |
min_machines_running | Minimum 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:
- Build the Docker image
- Run
npm run db:migrate(deployment continues only if migration succeeds) - Start the new container
- 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
main.tsmust listen on0.0.0.0:app.listen(port, '0.0.0.0')- Redis/Valkey must be publicly accessible (e.g. Upstash). AWS ElastiCache internal addresses won’t work
- Fly.io creates 2 machines by default (HA). To keep only 1:
fly machine destroy <id> --force