railway

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Railway Deployment

Railway 部署

Deploy and manage applications on Railway's platform.
在Railway平台部署和管理应用。

Quick Start

快速开始

bash
undefined
bash
undefined

Install 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
undefined
railway up
undefined

railway.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 = 3000
toml
[build]
builder = "nixpacks"
buildCommand = "npm run build"

[deploy]
startCommand = "npm start"
healthcheckPath = "/health"
healthcheckTimeout = 300
restartPolicyType = "on_failure"
restartPolicyMaxRetries = 3

[service]
internalPort = 3000

Nixpacks Configuration

Nixpacks 配置

toml
undefined
toml
undefined

nixpacks.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"
undefined

Environment Variables

环境变量

bash
undefined
bash
undefined

Set 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"
undefined
railway service railway variables set API_KEY="secret"
undefined

Database Services

数据库服务

PostgreSQL

PostgreSQL

bash
undefined
bash
undefined

Add PostgreSQL

Add PostgreSQL

railway add -d postgres
railway add -d postgres

Get connection string

Get connection string

railway variables get DATABASE_URL
undefined
railway variables get DATABASE_URL
undefined

Redis

Redis

bash
railway add -d redis
bash
railway add -d redis

Access via REDIS_URL

Access via REDIS_URL

undefined
undefined

MySQL

MySQL

bash
railway add -d mysql
bash
railway add -d mysql

Access via MYSQL_URL

Access via MYSQL_URL

undefined
undefined

Private Networking

私有网络

yaml
undefined
yaml
undefined

Services 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
undefined
DATABASE_HOST: ${{postgres.railway.internal}} DATABASE_PORT: 5432
undefined

Volumes (Persistent Storage)

存储卷(持久化存储)

bash
undefined
bash
undefined

Create 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
undefined
toml
undefined

railway.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
undefined

Multi-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 up
my-project/
├── api/
│   ├── railway.toml
│   └── ...
├── worker/
│   ├── railway.toml
│   └── ...
└── frontend/
    ├── railway.toml
    └── ...
分别部署:
bash
cd api && railway up
cd ../worker && railway up
cd ../frontend && railway up

Dockerfile Deploy

Dockerfile 部署

dockerfile
FROM node:18-alpine

WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .

EXPOSE 3000
CMD ["npm", "start"]
toml
undefined
dockerfile
FROM node:18-alpine

WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .

EXPOSE 3000
CMD ["npm", "start"]
toml
undefined

railway.toml

railway.toml

[build] builder = "dockerfile" dockerfilePath = "./Dockerfile"
undefined
[build] builder = "dockerfile" dockerfilePath = "./Dockerfile"
undefined

Health Checks

健康检查

typescript
// Express health endpoint
app.get('/health', (req, res) => {
  res.status(200).json({ 
    status: 'healthy',
    timestamp: new Date().toISOString()
  });
});
toml
undefined
typescript
// Express health endpoint
app.get('/health', (req, res) => {
  res.status(200).json({ 
    status: 'healthy',
    timestamp: new Date().toISOString()
  });
});
toml
undefined

railway.toml

railway.toml

[deploy] healthcheckPath = "/health" healthcheckTimeout = 100
undefined
[deploy] healthcheckPath = "/health" healthcheckTimeout = 100
undefined

Resources

资源链接