Fly.io
Fly.io 是一个云应用平台,允许你在全球范围内部署全栈应用。本指南将向你展示如何使用 Prisma Postgres 将 Node.js 应用部署到 Fly.io。
¥Fly.io is a cloud application platform that lets you deploy full-stack applications globally. This guide shows you how to deploy a Node.js application using Prisma Postgres to Fly.io.
先决条件
¥Prerequisites
-
一个 Fly.io 账户
-
已安装的 Fly CLI
¥The Fly CLI installed
-
使用 Prisma Postgres 的现有应用(参见 快速开始)
¥An existing application using Prisma Postgres (see Quickstart)
部署你的应用
¥Deploy your application
1. 在 postinstall 中生成 Prisma 客户端
¥ Generate Prisma Client in postinstall
请确保你的 package.json 文件包含一个 postinstall 脚本,以便在部署期间生成 Prisma 客户端:
¥Ensure your package.json includes a postinstall script to generate Prisma Client during deployment:
{
// ...
"scripts": {
// ...
"postinstall": "prisma generate"
}
}
2. 使用 Fly.io 启动你的应用
¥ Launch your app with Fly.io
在你的项目目录中运行:
¥From your project directory, run:
fly launch
按照提示配置你的应用。Fly.io 将自动检测你的 Node.js 应用并创建必要的配置。
¥Follow the prompts to configure your application. Fly.io will auto-detect your Node.js application and create the necessary configuration.
3. 设置数据库连接字符串
¥ Set your database connection string
将你的 Prisma Postgres DATABASE_URL 配置作为密钥添加到 Fly.io:
¥Add your Prisma Postgres DATABASE_URL as a secret in Fly.io:
fly secrets set DATABASE_URL="your-prisma-postgres-connection-string"
你可以在 .env 文件或 文件中找到 DATABASE_URL。
¥You can find your DATABASE_URL in your .env file or in the .
4. 部署
¥ Deploy
如果在 fly launch 期间尚未部署,请部署你的应用:
¥If not already deployed during fly launch, deploy your application:
fly deploy
部署完成后,你将看到一条包含应用 URL 的成功消息:
¥Once the deployment completes, you'll see a success message with your app's URL:
🎉 SUCCESS! Your app is live and ready to use! 🎉
Visit: https://your-app-name.fly.dev/
在 Fly.io 控制面板中,你的应用状态最初可能显示为 "待处理"。它通常会在几分钟内过渡到 "已部署"。
¥In the Fly.io dashboard, your app's status may show as "Pending" initially. It typically transitions to "Deployed" within a few minutes.
其他注意事项
¥Additional considerations
确保你的项目使用正确的环境变量
¥Ensure your project uses the correct environment variable
确保 prisma.config.ts 文件中的数据源已配置为使用 DATABASE_URL 环境变量:
¥Ensure that the data source in your prisma.config.ts file is configured to use the DATABASE_URL environment variable:
import 'dotenv/config';
import { defineConfig, env } from 'prisma/config';
export default defineConfig({
datasource: {
url: env('DATABASE_URL'),
},
schema: './prisma/schema.prisma',
});
在生产环境中运行迁移
¥Running migrations in production
要在已部署的 Fly.io 应用上运行迁移,你可以使用:
¥To run migrations on your deployed Fly.io app, you can use:
fly ssh console -C "npx prisma migrate deploy"
或者,你可以向 fly.toml 添加发布命令:
¥Or add a release command to your fly.toml:
[deploy]
release_command = "npx prisma migrate deploy"
扩展和区域
¥Scaling and regions
Fly.io 允许你扩展应用并将其部署到 多个区域 中。为了获得最佳性能,请将你的应用部署在靠近 Prisma Postgres 数据库区域的区域中。
¥Fly.io lets you scale and place your application in multiple regions. For optimal performance, deploy your app in a region close to your Prisma Postgres database region.
fly scale count 2 --region iad
故障排除
¥Troubleshooting
未找到 Prisma 模式或客户端未正确生成
¥Prisma schema not found or Client not generated correctly
如果你使用的是自定义 Dockerfile,并且构建失败是因为找不到 Prisma 客户端,则可能是以下两个原因之一:
¥If you're using a custom Dockerfile and your build fails because the Prisma Client cannot be found, it's likely due to one of two reasons:
-
操作顺序:
npm install(运行postinstall)在复制模式之前运行。¥Order of operations:
npm install(which runspostinstall) runs before the schema is copied. -
复制路径错误:模式被复制到根目录,而不是
prisma文件夹。¥Incorrect copy path: The schema is copied to the root instead of the
prismafolder.
解决方案:在你的 Dockerfile 中,运行 npm install 之前,将 prisma/ 目录复制到 ./prisma:
¥Solution: In your Dockerfile, copy the prisma/ directory to ./prisma before running npm install:
# ❌ Wrong order or path
COPY package*.json ./
COPY prisma . # Copies contents to root (wrong structure)
RUN npm install # postinstall runs but might fail or generate in wrong place
# ✅ Correct order and path
COPY package*.json ./
COPY prisma ./prisma # Copies to ./prisma folder (preserves structure)
RUN npm install # postinstall finds schema in expected location
COPY . .
更多信息
¥More information