Railway/Guides
Rust Axum アプリをデプロイする
Rust 製フレームワーク Axum で開発したアプリを Railway にデプロイする手順を、日本語で詳しく解説します。テンプレート、CLI、GitHub、Dockerfile など複数の方法を網羅。
著者: AIイノベーションズ 阿部隼也(X / Twitter)Rust Axum アプリをデプロイする
Axum は Rust 製の非同期 Web フレームワークで、tokio
と hyper
上に構築されています。安全性とパフォーマンスを両立しつつ、モジュール化された設計で高い生産性を実現します。
本ガイドでは、Axum アプリを Railway にデプロイする 4 つの方法を紹介します。
- テンプレートからワンクリックデプロイ
- GitHub リポジトリからデプロイ
- Railway CLI からデプロイ
- Dockerfile を使ったデプロイ
Axum アプリを作成する
既にプロジェクトがある場合はこの章をスキップしてください。
cargo new helloworld --bin
cd helloworld
cargo add axum
cargo add tokio --features full
次に src/main.rs
を以下のように書き換えます。
use axum::{ routing::get, Router };
use std::net::SocketAddr;
#[tokio::main]
async fn main() {
let app = Router::new().route("/", get(|| async { "Hello World, from Axum!" }));
let port: u16 = std::env::var("PORT").unwrap_or_else(|_| "3000".into()).parse().unwrap();
let addr = SocketAddr::from(([0, 0, 0, 0], port));
println!("🚀 Axum app listening on {}", addr);
axum::Server::bind(&addr).serve(app.into_make_service()).await.unwrap();
}
ローカルで実行
cargo run
ブラウザで http://localhost:3000
を開き、"Hello World, from Axum!" と表示されれば準備完了です。
Railway へのデプロイ方法
テンプレートからワンクリックデプロイ
Railway テンプレートマーケットプレイスに Axum テンプレートが公開されています。ボタン 1 つでインフラ設定からビルドまで自動で行われます。
CLI からデプロイ
railway init # プロジェクト初期化
railway up # デプロイ
railway domain # 公開 URL 発行
CLI はプロジェクトを圧縮してアップロードし、実行ログをリアルタイムに確認できます。
GitHub リポジトリからデプロイ
- Railway ダッシュボード → New Project → Deploy from GitHub repo
- リポジトリ選択 → Deploy
- デプロイ完了後、Settings → Networking で Generate Domain をクリック
Dockerfile を使ったデプロイ
以下は cargo-chef
を活用したマルチステージビルド例です。
FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef
WORKDIR /app
FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
FROM chef AS builder
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
COPY . .
RUN cargo build --release
FROM debian:bullseye-slim
WORKDIR /app
COPY --from=builder /app/target/release/helloworld ./helloworld
CMD ["./helloworld"]
次のステップ
- データベースとの連携 –
sqlx
を使って Postgres に接続 - 観測性の向上 –
tracing
と OpenTelemetry で分散トレースを導入
Rust × Axum × Railway で安全かつ高速なバックエンドを構築しましょう!
PR