railway
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRailway Deployment
Railway 部署
Deploy and manage applications on Railway's platform.
在Railway平台部署和管理应用。
Quick Start
快速开始
bash
undefinedbash
undefinedInstall Railway CLI
Install Railway CLI
npm i -g @railway/cli
npm i -g @railway/cli
Login
Login
railway login
railway login
Initialize project
Initialize project
railway init
railway init
Deploy
Deploy
railway up
undefinedrailway up
undefinedrailway.toml Configuration
railway.toml 配置
toml
[build]
builder = "nixpacks"
buildCommand = "npm run build"
[deploy]
startCommand = "npm start"
healthcheckPath = "/health"
healthcheckTimeout = 300
restartPolicyType = "on_failure"
restartPolicyMaxRetries = 3
[service]
internalPort = 3000toml
[build]
builder = "nixpacks"
buildCommand = "npm run build"
[deploy]
startCommand = "npm start"
healthcheckPath = "/health"
healthcheckTimeout = 300
restartPolicyType = "on_failure"
restartPolicyMaxRetries = 3
[service]
internalPort = 3000Nixpacks Configuration
Nixpacks 配置
toml
undefinedtoml
undefinednixpacks.toml
nixpacks.toml
[phases.setup]
nixPkgs = ["nodejs-18_x", "python311"]
[phases.install]
cmds = ["npm ci"]
[phases.build]
cmds = ["npm run build"]
[start]
cmd = "npm start"
undefined[phases.setup]
nixPkgs = ["nodejs-18_x", "python311"]
[phases.install]
cmds = ["npm ci"]
[phases.build]
cmds = ["npm run build"]
[start]
cmd = "npm start"
undefinedEnvironment Variables
环境变量
bash
undefinedbash
undefinedSet variable
Set variable
railway variables set DATABASE_URL="postgres://..."
railway variables set DATABASE_URL="postgres://..."
Set from file
Set from file
railway variables set < .env
railway variables set < .env
Link to service
Link to service
railway service
railway variables set API_KEY="secret"
undefinedrailway service
railway variables set API_KEY="secret"
undefinedDatabase Services
数据库服务
PostgreSQL
PostgreSQL
bash
undefinedbash
undefinedAdd PostgreSQL
Add PostgreSQL
railway add -d postgres
railway add -d postgres
Get connection string
Get connection string
railway variables get DATABASE_URL
undefinedrailway variables get DATABASE_URL
undefinedRedis
Redis
bash
railway add -d redisbash
railway add -d redisAccess via REDIS_URL
Access via REDIS_URL
undefinedundefinedMySQL
MySQL
bash
railway add -d mysqlbash
railway add -d mysqlAccess via MYSQL_URL
Access via MYSQL_URL
undefinedundefinedPrivate Networking
私有网络
yaml
undefinedyaml
undefinedServices can communicate via internal DNS
Services can communicate via internal DNS
Format: ${{service-name}}.railway.internal
Format: ${{service-name}}.railway.internal
Example: API calling database service
Example: API calling database service
DATABASE_HOST: ${{postgres.railway.internal}}
DATABASE_PORT: 5432
undefinedDATABASE_HOST: ${{postgres.railway.internal}}
DATABASE_PORT: 5432
undefinedVolumes (Persistent Storage)
存储卷(持久化存储)
bash
undefinedbash
undefinedCreate volume
Create volume
railway volume create my-data
railway volume create my-data
Mount in service
Mount in service
railway volume attach my-data:/app/data
In code:
```javascript
// Data persists across deploys
const dataPath = '/app/data';
fs.writeFileSync(`${dataPath}/file.json`, JSON.stringify(data));railway volume attach my-data:/app/data
代码中使用:
```javascript
// Data persists across deploys
const dataPath = '/app/data';
fs.writeFileSync(`${dataPath}/file.json`, JSON.stringify(data));Cron Jobs
定时任务
toml
undefinedtoml
undefinedrailway.toml
railway.toml
[deploy]
startCommand = "node cron.js"
cronSchedule = "0 */6 * * *" # Every 6 hours
undefined[deploy]
startCommand = "node cron.js"
cronSchedule = "0 */6 * * *" # Every 6 hours
undefinedMulti-Service Setup
多服务部署
my-project/
├── api/
│ ├── railway.toml
│ └── ...
├── worker/
│ ├── railway.toml
│ └── ...
└── frontend/
├── railway.toml
└── ...Deploy each:
bash
cd api && railway up
cd ../worker && railway up
cd ../frontend && railway upmy-project/
├── api/
│ ├── railway.toml
│ └── ...
├── worker/
│ ├── railway.toml
│ └── ...
└── frontend/
├── railway.toml
└── ...分别部署:
bash
cd api && railway up
cd ../worker && railway up
cd ../frontend && railway upDockerfile Deploy
Dockerfile 部署
dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["npm", "start"]toml
undefineddockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["npm", "start"]toml
undefinedrailway.toml
railway.toml
[build]
builder = "dockerfile"
dockerfilePath = "./Dockerfile"
undefined[build]
builder = "dockerfile"
dockerfilePath = "./Dockerfile"
undefinedHealth Checks
健康检查
typescript
// Express health endpoint
app.get('/health', (req, res) => {
res.status(200).json({
status: 'healthy',
timestamp: new Date().toISOString()
});
});toml
undefinedtypescript
// Express health endpoint
app.get('/health', (req, res) => {
res.status(200).json({
status: 'healthy',
timestamp: new Date().toISOString()
});
});toml
undefinedrailway.toml
railway.toml
[deploy]
healthcheckPath = "/health"
healthcheckTimeout = 100
undefined[deploy]
healthcheckPath = "/health"
healthcheckTimeout = 100
undefinedResources
资源链接
- Railway Docs: https://docs.railway.app
- Railway CLI: https://docs.railway.app/develop/cli
- Railway 文档: https://docs.railway.app
- Railway CLI: https://docs.railway.app/develop/cli