Run a Rust Rocket app
This example uses Rocket
, a popular Rust web framework.
To run this example, follow these steps:
-
Install the
kraft
CLI tool and a container runtime engine, e.g. Docker. -
Clone the
examples
repository andcd
into theexamples/http-rust1.75-rocket0.5/
directory:
git clone https://github.com/kraftcloud/examplescd examples/http-rust1.75-rocket0.5
Make sure to log into Unikraft Cloud by setting your token and a metro close to you.
We use fra0
(Frankfurt, π©πͺ) in this guide:
# Set Unikraft Cloud access tokenexport UKC_TOKEN=token# Set metro to Frankfurt, DEexport UKC_METRO=fra0
When done, invoke the following command to deploy this application on Unikraft Cloud:
kraft cloud deploy -p 443:8080 .
The output shows the instance URL and other details:
[β] Deployed successfully! β βββββββββββ name: http-rust175-rocket05-tuwq3 βββββββββββ uuid: b6fe13e4-93b7-402b-bdec-1bc4d81bc275 ββββββββββ state: running ββββββββββββ url: https://empty-bobo-n3htmpye.fra0.kraft.host ββββββββββ image: http-rust175-rocket05@sha256:23a7a6e155758e6e8f75e9570f0aec5fb744f08c1bad2454d7386367c5ea45d6 ββββββ boot time: 17.41 ms βββββββββ memory: 128 MiB ββββββββ service: empty-bobo-n3htmpye βββ private fqdn: http-rust175-rocket05-tuwq3.internal βββββ private ip: 172.16.6.6 βββββββββββ args: /server
In this case, the instance name is http-rust175-rocket05-tuwq3
and the URL is https://empty-bobo-n3htmpye.fra0.kraft.host
.
They are different for each run.
Use curl
to query any of the Rocket serverβs paths, for example:
curl https://empty-bobo-n3htmpye.fra0.kraft.host/wave/Rocketeer/100
π Hello, 100 year old named Rocketeer!
At any point in time, you can list information about the instance:
kraft cloud instance list
NAME FQDN STATE CREATED AT IMAGE MEMORY ARGS BOOT TIMEhttp-rust175-rocket05-tuwq3 empty-bobo-n3htmpye.fra0.k... running 1 minute ago http-rust175-rocket05@sha256:23a7a6e155758e6e8f75e9570f0aec5fb744... 128 MiB /server 17412us
When done, you can remove the instance:
kraft cloud instance remove http-rust175-rocket05-tuwq3
Customize your Application
To customize the application, update the files in the repository, listed below:
src/main.rs
: the actual serverCargo.toml
: the Cargo package manager configuration fileKraftfile
: the Unikraft Cloud specificationDockerfile
: the Docker-specified application filesystem
spec: v0.6
runtime: base:latest
rootfs: ./Dockerfile
cmd: ["/server"]
#[macro_use] extern crate rocket;
#[cfg(test)] mod tests;
#[derive(FromFormField)]enum Lang {#[field(value = "en")]English,#[field(value = "ru")]#[field(value = "ΡΡ")]Russian}
#[derive(FromForm)]struct Options<'r> {emoji: bool,name: Option<&'r str>,}
// Try visiting:// http://127.0.0.1:8000/hello/world#[get("/world")]fn world() -> &'static str {"Hello, world!"}
// Try visiting:// http://127.0.0.1:8000/hello/ΠΌΠΈΡ#[get("/ΠΌΠΈΡ")]fn mir() -> &'static str {"ΠΡΠΈΠ²Π΅Ρ, ΠΌΠΈΡ!"}
// Try visiting:// http://127.0.0.1:8000/wave/Rocketeer/100#[get("/<name>/<age>")]fn wave(name: &str, age: u8) -> String {format!("π Hello, {} year old named {}!", age, name)}
// Note: without the `..` in `opt..`, we'd need to pass `opt.emoji`, `opt.name`.//// Try visiting:// http://127.0.0.1:8000/?emoji// http://127.0.0.1:8000/?name=Rocketeer// http://127.0.0.1:8000/?lang=ΡΡ// http://127.0.0.1:8000/?lang=ΡΡ&emoji// http://127.0.0.1:8000/?emoji&lang=en// http://127.0.0.1:8000/?name=Rocketeer&lang=en// http://127.0.0.1:8000/?emoji&name=Rocketeer// http://127.0.0.1:8000/?name=Rocketeer&lang=en&emoji// http://127.0.0.1:8000/?lang=ru&emoji&name=Rocketeer#[get("/?<lang>&<opt..>")]fn hello(lang: Option<Lang>, opt: Options<'_>) -> String {let mut greeting = String::new();if opt.emoji { greeting.push_str("π ");}
match lang { Some(Lang::Russian) => greeting.push_str("ΠΡΠΈΠ²Π΅Ρ"), Some(Lang::English) => greeting.push_str("Hello"), None => greeting.push_str("Hi"),}
if let Some(name) = opt.name { greeting.push_str(", "); greeting.push_str(name);}
greeting.push('!');greeting}
#[launch]fn rocket() -> _ {rocket::build() .mount("/", routes![hello]) .mount("/hello", routes![world, mir]) .mount("/wave", routes![wave])}
[package]name = "hello"version = "0.0.0"edition = "2021"publish = false
[dependencies]rocket = "0.5.0"
FROM rust:1.75.0-bookworm AS build
RUN cargo new --bin appWORKDIR /appCOPY Cargo.toml ./COPY Rocket.toml ./COPY src ./srcRUN cargo build --release
FROM scratch
COPY ./Rocket.toml /Rocket.tomlCOPY --from=build /app/target/release/hello /serverCOPY --from=build /lib/x86_64-linux-gnu/libc.so.6 /lib/x86_64-linux-gnu/COPY --from=build /lib/x86_64-linux-gnu/libm.so.6 /lib/x86_64-linux-gnu/COPY --from=build /lib/x86_64-linux-gnu/libgcc_s.so.1 /lib/x86_64-linux-gnu/COPY --from=build /lib64/ld-linux-x86-64.so.2 /lib64/
The following options are available for customizing the application:
-
If only updating the implementation in the
src/main.rs
source file, no other change is required. -
If new files are added, these have to be copied in the application filesystem, using the
COPY
command in theDockerfile
. If new Rust source code files are added, be sure to configure required dependencies in theCargo.toml
file. -
If a new executable is built, update the
cmd
line in theKraftfile
and replace/server
with the path to the new executable. -
More extensive changes may require expanding the
Dockerfile
with additionalDockerfile
commands.
Learn More
Use the --help
option for detailed information on using Unikraft Cloud:
kraft cloud --help
Or visit the CLI Reference.