Deploy to Railway & Fly.io

Deploying on Fly.io

35 min

Install and launch

# install flyctl (see Fly's docs for your OS), then:
fly auth login
fly launch            # detects the app, generates fly.toml and a Dockerfile if needed
fly deploy            # builds the image and deploys machines
fly open

fly.toml

app = "pandadev-demo"
primary_region = "lhr"

[build]

[deploy]
  release_command = "flask --app wsgi db upgrade"

[env]
  FLASK_ENV = "production"
  PORT = "8080"

[http_service]
  internal_port = 8080
  force_https = true
  auto_stop_machines = "stop"
  auto_start_machines = true
  min_machines_running = 1

[[http_service.checks]]
  interval = "15s"
  timeout = "5s"
  method = "GET"
  path = "/api/health"
  • internal_port must match the port your app listens on.
  • release_command runs once per deploy before new machines receive traffic: ideal for migrations.
  • Auto stop/start saves money by stopping idle machines; keep at least one running if cold starts matter.

Secrets

fly secrets set SECRET_KEY=$(python -c "import secrets;print(secrets.token_hex(32))") STRIPE_SECRET_KEY=sk_live_...
fly secrets list           # names only, values are never shown

Setting secrets triggers a rolling restart.

Volumes

fly volumes create data --size 3 --region lhr
[mounts]
  source = "data"
  destination = "/data"

A volume is attached to one machine in one region and is not automatically replicated. Back it up, and prefer managed databases and object storage for important data.

Logs and access

fly logs
fly status
fly ssh console          # shell inside a running machine

Exercise

Launch a Dockerised Flask app on Fly.io with a health check, a release command for migrations, secrets set with fly secrets, and a volume mounted for a SQLite-based demo; then switch the demo to a managed Postgres.

Check yourself

  • What runs in release_command, and when?
  • Why must internal_port match your app?
  • What are the limits of a Fly volume?

Votre progression est enregistrée dans ce navigateur uniquement.

Envoyer pour relecture (10 points)