Deploy a Rust Rocket App
Rocket is a web framework for Rust that makes it simple to write fast, type-safe, secure web applications with incredible usability, productivity and performance.
This guide covers how to deploy a Rocket app to Railway in four ways:
Now, let's create a Rocket app! 🚀
Create a Rocket App
Note: If you already have a Rocket app locally or on GitHub, you can skip this step and go straight to the Deploy Rocket App to Railway.
To create a new Rocket app, ensure that you have Rust installed on your machine.
Run the following command in your terminal to create a new Rust app:
cargo new helloworld --bin
The command creates a new binary-based Cargo project in a helloworld
directory.
Next, cd
into the directory and add Rocket as a dependency by running the following command:
cargo add rocket
This will add Rocket as a dependency, and you’ll see it listed in your Cargo.toml
file.
Modify the Application File
Next, open the app in your IDE and navigate to the src/main.rs
file.
Replace the content with the code below:
#[macro_use]
extern crate rocket;
#[get("/")]
fn index() -> &'static str {
"Hello world, Rocket!"
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index])
}
The code above uses the Rocket framework to create a basic web server that responds to HTTP requests. It defines a simple route using the #[get("/")]
macro, which tells Rocket to handle GET requests to the root URL (/)
.
The index()
function is the handler for this route and returns a static string, "Hello world, Rocket!", which will be sent as the response when the root URL is accessed.
The #[launch]
attribute on the rocket()
function marks it as the entry point to launch the application. Inside rocket()
, the server is built with rocket::build()
and the index route is mounted to the root path /
using mount()
.
When the application runs, it listens for incoming requests and serves the "Hello world, Rocket!" response for requests made to the root URL, demonstrating a simple routing and response mechanism in Rocket.
Run the Rocket App locally
Run the following command in the helloworld
directory via the terminal:
cargo run
All the dependencies will be installed and your app will be launched.
Open your browser and go to http://localhost:8000
to see your app.
Deploy the Rocket App to Railway
Railway offers multiple ways to deploy your Rocket app, depending on your setup and preference.
One-Click Deploy from a Template
If you’re looking for the fastest way to get started, the one-click deploy option is ideal.
Click the button below to begin:
We highly recommend that you eject from the template after deployment to create a copy of the repo on your GitHub account.
Note: You can also choose from a variety of Rocket templates created by the community.
Deploy from the CLI
-
Install the Railway CLI:
- Install the CLI and authenticate it using your Railway account.
-
Initialize a Railway Project:
- Run the command below in your Rocket app directory.
railway init
- Follow the prompts to name your project.
- After the project is created, click the provided link to view it in your browser.
- Run the command below in your Rocket app directory.
-
Deploy the Application:
- Use the command below to deploy your app:
railway up
- This command will scan, compress and upload your app's files to Railway. You’ll see real-time deployment logs in your terminal.
- Use the command below to deploy your app:
-
Set Up a Public URL:
- Navigate to the Networking section under the Settings tab of your new service.
- Click Generate Domain to create a public URL for your app.
Note: You'll come across a 502 error where your application doesn't respond. We'll fix that in the next step.
-
Configure Rocket app to accept non-local connections:
- Rocket apps need to be configured to accept external connections by listening on the correct address, which is typically
0.0.0.0
. You can easily do this by setting the address through the environment variable. Run the following command to set the Rocket address to0.0.0.0
:railway variables --set "ROCKET_ADDRESS=0.0.0.0"
- Rocket apps need to be configured to accept external connections by listening on the correct address, which is typically
-
Redeploy the Service:
- Run
railway up
again to trigger a redeployment of the service.
- Run
-
Verify the Deployment:
- Once the deployment completes, go to View logs to check if the server is running successfully. Access your public URL again and you should see your app working well.
Deploy from a GitHub Repo
To deploy a Rocket app to Railway directly from GitHub, follow the steps below:
-
Create a New Project on Railway:
- Go to Railway to create a new project.
-
Deploy from GitHub:
- Select Deploy from GitHub repo and choose your repository.
- If your Railway account isn’t linked to GitHub yet, you’ll be prompted to do so.
- Select Deploy from GitHub repo and choose your repository.
-
Add Environment Variables:
- Click Add Variables, then add
ROCKET_ADDRESS
with the value0.0.0.0
. This allows your Rocket app to accept external connections by listening on0.0.0.0
.
- Click Add Variables, then add
-
Deploy the App:
- Click Deploy to start the deployment process.
- Once the deployed, a Railway service will be created for your app, but it won’t be publicly accessible by default.
-
Verify the Deployment:
- Once the deployment completes, go to View logs to check if the server is running successfully.
Note: During the deployment process, Railway will automatically detect that it’s a Rust app.
-
Set Up a Public URL:
- Navigate to the Networking section under the Settings tab of your new service.
- Click Generate Domain to create a public URL for your app.
Use a Dockerfile
- Create a
Dockerfile
in thehelloworld
or Rocket app's root directory. - Add the content below to the
Dockerfile
:FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef # Create and change to the app directory. WORKDIR /app FROM chef AS planner COPY . ./ RUN cargo chef prepare --recipe-path recipe.json FROM chef AS builder COPY /app/recipe.json recipe.json # Build dependencies - this is the caching Docker layer! RUN cargo chef cook --release --recipe-path recipe.json # Build application COPY . ./ RUN cargo build --release CMD ["./target/release/helloworld"]
- Either deploy via the CLI or from GitHub.
Railway automatically detects the Dockerfile
, and uses it to build and deploy the app.
Note: Railway supports also deployment from public and private Docker images.
This guide covers the main deployment options on Railway. Choose the approach that suits your setup, and start deploying your Rocket apps seamlessly!
Next Steps
Explore these resources to learn how you can maximize your experience with Railway:
Edit this file on GitHub