Skip to content

Commit

Permalink
feat: rust dockerfile example
Browse files Browse the repository at this point in the history
  • Loading branch information
elijah-rou committed Jan 7, 2025
1 parent 3f55543 commit 0ad5395
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 11 deletions.
95 changes: 95 additions & 0 deletions cerebrium/endpoints/custom-dockerfiles.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
title: "Custom Dockerized Runtimes"
description: "Run generic dockerized applications on Cerebrium"
---

For even greater flexibility, you are able to build your own dockerized applications with Cerebrium! You can build anything from standard Python to
compiled Rust, as long as you supply a Dockerfile to build your application with.

## Building Custom Dockerized applications

Here's a straightforward Rust API server in the Axon Framework to demonstrate how this works:

```rust
use axum::{
routing::{get, post},
Json, Router,
};
use serde_json::json;
use std::net::SocketAddr;
use tracing::Level;

async fn hello() -> Json<serde_json::Value> {
Json(json!({ "message": "Hello Cerebrium!" }))
}

async fn health() -> &'static str {
"Ok"
}

#[tokio::main]
async fn main() {
let app = Router::new()
.route("/hello", post(hello))
.route("/health", get(health));
let addr = SocketAddr::from(([127, 0, 0, 1], 5000));
tracing::info!("Listening on {}", addr);
axum::Server::bind(&addr.parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}
```

We can supply a Dockerfile which Cerebrium will use to build this application. This is just a regular, multi-stage Dockerfile. However,
it should preferably include a `CMD` or `ENTRYPOINT` clause, so that the container can be run correctly.

```dockerfile
# Build Stage
FROM rust:bookworm as builder
WORKDIR /usr/src/app
COPY Cargo.toml Cargo.lock ./
RUN cargo fetch

# Copy the rest of the application source code and Build
COPY . .
RUN cargo build --release


# Runtime Stage
FROM scratch
COPY --from=builder /usr/src/app/target/release/axum_server /app
EXPOSE 5000
CMD ["/app"]
```

Configure this application in `cerebrium.toml` by adding a custom runtime section, similarly to the custom Python webserver:

```toml
[cerebrium.runtime.custom]
port = 5000
entrypoint = ["main:app", "--host", "0.0.0.0", "--port", "5000"]
healthcheck_endpoint = "/health"
dockerfile = "./Dockerfile"
```

The configuration requires three key parameters:

- `entrypoint`: The command that starts your server. This is optional, and will override the `CMD` specified in your Dockerfile
- `port`: The port your server listens on
- `healthcheck_endpoint`: The endpoint that confirms server health. If empty, will default to a TCP ping of your specified port
- `dockerfile_path`: The relative path of the project Dockerfile used to build the application

<Info>
If a dockerfile path is specified, you must take care to install all the
dependencies your app needs in the Dockerfile, as well as run any commands.
All dependencies specified under `cerebrium.dependencies.*` will be ignored,
and `cerebrium.deployment.shell_commands` &
`cerebrium.deployment.pre_build_commands` will not be run.
</Info>

Our [Dockerized App Example](https://github.com/CerebriumAI/examples) provides a complete implementation.

```
```
2 changes: 1 addition & 1 deletion cerebrium/endpoints/custom-web-servers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The configuration requires three key parameters:

- `entrypoint`: The command that starts your server
- `port`: The port your server listens on
- `healthcheck_endpoint`: The endpoint that confirms server health
- `healthcheck_endpoint`: The endpoint that confirms server health. If empty, will default to a TCP ping of your specified port

<Info>
For ASGI applications like FastAPI, include the appropriate server package
Expand Down
40 changes: 30 additions & 10 deletions mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"light": "/logo/light.svg",
"dark": "/logo/dark.svg"
},
"versions": ["v4"],
"versions": [
"v4"
],
"favicon": "/favicon.png",
"colors": {
"primary": "#EB3A6F",
Expand Down Expand Up @@ -81,7 +83,9 @@
},
{
"group": "Container Images",
"pages": ["cerebrium/container-images/defining-container-images"]
"pages": [
"cerebrium/container-images/defining-container-images"
]
},
{
"group": "GPUs and Other Resources",
Expand All @@ -100,7 +104,9 @@
},
{
"group": "Deployments",
"pages": ["cerebrium/deployments/ci-cd"]
"pages": [
"cerebrium/deployments/ci-cd"
]
},
{
"group": "Endpoints",
Expand All @@ -111,16 +117,21 @@
"cerebrium/endpoints/websockets",
"cerebrium/endpoints/webhook",
"cerebrium/endpoints/async",
"cerebrium/endpoints/custom-web-servers"
"cerebrium/endpoints/custom-web-servers",
"cerebrium/endpoints/custom-dockerfiles"
]
},
{
"group": "Storage",
"pages": ["cerebrium/storage/managing-files"]
"pages": [
"cerebrium/storage/managing-files"
]
},
{
"group": "Integrations",
"pages": ["cerebrium/integrations/vercel"]
"pages": [
"cerebrium/integrations/vercel"
]
},
{
"group": "Other concepts",
Expand All @@ -139,7 +150,9 @@
"v4/examples/featured",
{
"group": "Advanced Concepts",
"pages": ["v4/examples/mistral-vllm"]
"pages": [
"v4/examples/mistral-vllm"
]
},
{
"group": "Large Language Models",
Expand All @@ -150,7 +163,9 @@
},
{
"group": "Integrations",
"pages": ["v4/examples/langchain-langsmith"]
"pages": [
"v4/examples/langchain-langsmith"
]
},
{
"group": "Voice",
Expand All @@ -162,11 +177,16 @@
},
{
"group": "Image & Video",
"pages": ["v4/examples/comfyUI", "v4/examples/sdxl"]
"pages": [
"v4/examples/comfyUI",
"v4/examples/sdxl"
]
},
{
"group": "Python Apps",
"pages": ["v4/examples/asgi-gradio-interface"]
"pages": [
"v4/examples/asgi-gradio-interface"
]
}
]
},
Expand Down

0 comments on commit 0ad5395

Please sign in to comment.