FerryDocs

Rust

Ferry compiles a Rust app with cargo, then runs the binary in a small image.

Edit on GitHub
cargo buildYour sourceCargo.toml, src/Build imageRust and cargoCacheregistry, targetBinaryRuntime imageDebian, small

Ferry finds Cargo.toml and starts the build image: rust:1-slim-bookworm.

1 / 4

A minimal example

Cargo.toml
Cargo.lock
main.rs
Cargo.toml
[package]
name = "hello"
version = "0.1.0"
edition = "2021"
src/main.rs
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());
    }
}
Terminal
ferry up hello-rust --follow

What Ferry generates

StepWhat Ferry generates
Build imagerust: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.
BuildYour build command. If there is none: cargo build --release.
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 imagedebian: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.

On this page