GuidesLanguage guides
Rust
Ferry compiles a Rust app with cargo, then runs the binary in a small image.
Ferry finds Cargo.toml and starts the build image: rust:1-slim-bookworm.
1 / 4
A minimal example
Cargo.toml
Cargo.lock
main.rs
[package]
name = "hello"
version = "0.1.0"
edition = "2021"use std::io::{Read, Write};
use std::net::TcpListener;
fn main() {
let port = std::env::var("PORT").unwrap_or_else(|_| "3000".to_string());
let listener = TcpListener::bind(format!("0.0.0.0:{port}")).expect("bind");
println!("listening on :{port}");
for stream in listener.incoming() {
let Ok(mut stream) = stream else { continue };
let mut buf = [0u8; 1024];
let _ = stream.read(&mut buf);
let body = "Hello from Rust on Ferry\n";
let _ = write!(stream, "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len());
}
}ferry up hello-rust --followWhat Ferry generates
| Step | What Ferry generates |
|---|---|
| Build image | rust:1-slim-bookworm. Ferry adds pkg-config libssl-dev when Cargo.lock contains openssl-sys. Ferry keeps the Cargo registry and the target directory in a cache between builds. |
| Build | Your build command. If there is none: cargo build --release. |
| Binary | default-run of [package]. If there is none: the package name (with src/main.rs), then the first [[bin]], then the first file of src/bin. Ferry copies it to /usr/local/bin. |
| Runtime image | debian:bookworm-slim with ca-certificates (and libssl3 for OpenSSL). Your source tree is in /app. |
| Start | /usr/local/bin/<binary>. A workspace with no root package needs --start-cmd with the name of the binary. |