Deploying a Rocket App
Rocket is a fast, type-safe web framework for Rust, designed for ease of use and high performance. It provides a simple API, strong compile-time guarantees, and modern features, making it a great choice for building secure and scalable web applications in Rust.
This guide explains how to deploy a Rocket application to Klutch.sh, both with and without a Dockerfile. It also covers installation and provides sample code to get started.
Prerequisites
- Rust (latest stable)
- Cargo installed
- Git and GitHub account
- Klutch.sh account
Getting Started: Install Rocket
- Create a new Rocket app:
Terminal window cargo new my-rocket-appcd my-rocket-app - Add Rocket to your
Cargo.toml
:[dependencies]rocket = "0.5" - Create a basic Rocket app (
src/main.rs
):#[macro_use] extern crate rocket;#[get("/")]fn index() -> &'static str {"Hello from Rocket on Klutch.sh!"}#[launch]fn rocket() -> _ {let port = std::env::var("PORT").unwrap_or("8000".to_string()).parse().unwrap();rocket::build().mount("/", routes![index]).configure(rocket::Config {port,..rocket::Config::default()})} - Test locally:
Visit http://localhost:8000 to see your app running.
Terminal window cargo run
Deploying Without a Dockerfile
- Push your Rocket app to a GitHub repository.
- Log in to Klutch.sh.
- Create a new project and give it a name.
- Create a new app:
- Select your Rocket GitHub repository and branch
- Set the port to route traffic (usually 8000 for Rocket)
- Choose region, compute, number of instances, and add any environment variables
- Add a start command in your app settings:
Terminal window cargo run --release - Click “Create” to deploy. Klutch.sh will build and deploy your app automatically.
Deploying With a Dockerfile
- Add a
Dockerfile
to your project root. Example:# Use official Rust image for buildFROM rust:latest AS builderWORKDIR /appCOPY . .RUN cargo build --release# Use minimal image for runningFROM debian:bullseye-slimWORKDIR /appCOPY --from=builder /app/target/release/my-rocket-app .# Expose port (match your Rocket app)EXPOSE 8000# Start the appCMD ["./my-rocket-app"] - Push your code (with Dockerfile) to GitHub.
- In Klutch.sh, follow the same steps to create a project and app, but select the Dockerfile option when prompted.
- Set the service details and environment variables as needed.
- Click “Create” to deploy. Klutch.sh will build your Docker image and deploy your app.
Note: Your Rocket app should always listen on the PORT
environment variable as shown above.
Resources
Deploying to Klutch.sh is simple and flexible. Choose the method that best fits your workflow and project requirements.