Run a Rust Axum App

Getting an application running on Fly.io is essentially working out how to package it as a deployable image. Once packaged, it can be deployed to the Fly.io global application platform.

In this guide we’ll learn how to deploy a Rust Axum application on Fly.io.

Deploying an Axum app on Fly.io is really straightforward! With the help of the cargo chef, we get great build times and small images.

Deploy an Axum App


First, install flyctl, your Fly.io app command center, and sign up to Fly.io if you haven’t already.

If you don’t already have an existing Axum application, you can create one quickly with cargo:

cargo new axum-app
cd axum-app

Then we have to add some dependencies to the project:

cargo add axum
cargo add tokio -F macros -F rt-multi-thread

Now, let’s create a simple Axum app in src/main.rs:

use axum::{routing::get, Router};

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(|| async { "Hello, World!" }));
    let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

This will display a “Hello, World!” message when you visit the root URL. Take note that we serve the app on port 8080.

We can confirm everything works fine by running cargo run and checking out http://localhost:8080.

Now we add a Dockerfile to the project:

FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef
WORKDIR /app

FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

FROM chef AS builder 
COPY --from=planner /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 --bin axum-app

# We do not need the Rust toolchain to run the binary!
FROM debian:bookworm-slim AS runtime
WORKDIR /app
COPY --from=builder /app/target/release/axum-app /usr/local/bin
ENTRYPOINT ["/usr/local/bin/axum-app"] 

Modify the dockerfile to match your project name (in this case that’s axum-app).

Finally, deploy the app!

fly launch
Scanning source code
Detected a Dockerfile app
Warning: This organization has no payment method, turning off high availability
Creating app in [redacted]/fly-rust-axum-starter
We're about to launch your app on Fly.io. Here's what you're getting:

Organization: Your Name              (fly launch defaults to the personal org)
Name:         axum-app               (derived from your directory name)
Region:       Amsterdam, Netherlands (this is the fastest region for you)
App Machines: shared-cpu-1x, 1GB RAM (most apps need about 1GB of RAM)
Postgres:     <none>                 (not requested)
Redis:        <none>                 (not requested)
Sentry:       false                  (not requested)

...

==> Building image
...
==> Building image with Docker
...

Watch your deployment at https://fly.io/apps/[app-name]/monitoring
...

Visit your newly deployed app at https://[app-name].fly.dev/

This will generate a fly.toml file with the configuration for your app. Refer to the fly.toml docs for more configuration options.

To deploy a new version of your app, simply run fly deploy in the project directory.

You can check out the full (yet minimal) example in this GitHub repository for a reference.