How to deploy a Rust App?
NOTE: Spheron Compute offers the flexibility to create custom configurations for your instance.
Rust is a modern systems programming language that focuses on memory safety, concurrency, and high performance. It empowers developers to create secure and scalable applications, making it an ideal choice for systems-level programming tasks.
In this guide, you’ll configure a Rust app and deploy it to Spheron Compute using Docker.
Prerequisites
To successfully follow this guide, you will need the following:
- A DockerHub (opens in a new tab) account.
- Rust (opens in a new tab) installed on your local machine.
- A text editor. You can use Visual Studio Code (opens in a new tab) or your favorite text editor.
Step 1: Create a Rust App
-
Create a new Rust project by running the following command:
cargo new rustapp
-
Change into the project directory:
cd rustapp
-
Open the Cargo.toml file and add the following dependencies under the [dependencies] section:
[dependencies] rocket = "=0.5.0-rc.3"
-
Open the myproject/urls.py file and replace its contents with the following code:
#[macro_use] extern crate rocket; #[get("/hello")] fn hello() -> &'static str { "Hello, World!" } #[launch] fn rocket() -> _ { rocket::build().mount("/", routes![hello]) }
-
Start the app by running the following command:
cargo run
-
Open your web browser and visit http://localhost:8000/hello (opens in a new tab). You should see the message "Hello, World!" displayed.
Check out the example here. (opens in a new tab)
Step 2: Create a Dockerfile
Here's what the dockerfile for this Rust app will look like:
# Use an official Rust base image
FROM rust:latest
# Create a new directory for the app
WORKDIR /usr/src/rustapp
# Copy the Cargo.toml and Cargo.lock files to the container
COPY Cargo.toml Cargo.lock ./
# Copy the source code to the container
COPY src ./src
# Build the app
RUN cargo build --release
# Expose the port that the app listens on
EXPOSE 8000
# Run the app
CMD ["cargo", "run", "--release"]
Step 3: Set default platform for the docker build
Docker images built with Apple Silicon (or another ARM64 based architecture) can create issues when deploying the images to a Linux or Windows-based AMD64 environment. Before running the docker build command, run this command in your terminal:
export DOCKER_DEFAULT_PLATFORM=linux/amd64
Step 4: Build a Docker image
To build the Docker image:
- Save the above Dockerfile in the root directory of your Rust app.
- Open a terminal and navigate to the root directory of your project, where the Dockerfile is located.
- Run the following command to build the Docker image:
docker build -t rustapp .
- After the build process completes, you can run a container based on the image using the following command:
docker run -p 8000:8000 rustapp
Step 5: Push the app to DockerHub
To push an image, you first need to create a repository on Docker Hub.
Create a repo
To create a repository on Docker Hub:
- Sign up (opens in a new tab) or Sign in to Docker Hub (opens in a new tab).
- Select the Create Repository button.
- For the repo name, use rustapp. Make sure the Visibility is Public.
- Select the Create button.
Push the image
- Login to the Docker Hub using the command docker login -u YOUR-USER-NAME.
- Use the docker tag command to give the rustapp image a new name. Be sure to swap out YOUR-USER-NAME with your Docker ID.
docker tag rustapp YOUR-USER-NAME/rustapp
- Now try your push command again. If you’re copying the value from Docker Hub, you can drop the tagname portion, as you didn’t add a tag to the image name. If you don’t specify a tag, Docker will use a tag called latest.
docker push YOUR-USER-NAME/rustapp
Here's what a Docker Image will look like on Docker Hub:
Check out this docker image here. (opens in a new tab)
Step 6: Run on Spheron Compute
To run your app on Spheron:
- Click "New Cluster" on the top right corner.
- Choose "Compute" to use CPU-based instances for running containers.
- Choose your desired Compute Type option under Compute Type.
- Select Import from Docker Hub.
- Enter the names for your cluster and docker image.
- Then, Add the tag and Click "Next."
- When selecting a region, we recommend starting by trying to deploy in a region closer to you. If you encounter any issues, you can consider switching to other regions. Choosing a region closer to you can improve performance and reduce latency. Click here to know more.
- Spheron will automatically select the recommended plan for the specific template. If you intend to move forward with the recommended plan, Create new Port Policy Mapping and just Click "Deploy" to initiate deployment.
- Select the instance plan that suits your needs. You can use the "Create Custom Plan" toggle to create custom plans for your CPU based instance.
- Configure Storage (SSD) plan for your instance. Use the "Add Persistent Storage" toggle to add persistent storage for your instance.
- Create new Port Policy Mapping. Add the container port, and Select the exposed port you want to map it to. Click here to know more.
- Add Environment Variable, if any.
- Add Secret Environment Variable if the value is a secret key. It will not be saved in the database. Click here to know more.
- You can add advanced configuration if required. Click here to know more.
- You can add health checkup if required. Click here to know more.
- Click "Deploy" to initiate deployment.
NOTE: Spheron supports only public docker images at the moment.
Verify Installation
The Rust App can be accessed only after the Compute Instance is provisioned. Thus, you need to wait for the installation to complete before you can start using the app. Your Apps can be verified for successful installation using the instructions below, while others may require different procedures.
Follow these instructions to verify the installation:
- Attempt to access the app
An App has an estimated deployment time of about 1-2 minutes. If you can successfully access it, the installation has been completed successfully. You can connect using the connection URL of the instance, which will also be provided after the instance is provisioned. - Check instance logs and events
After successfully deploying your Rust App, it will produce logs and events, which you can check for any issues or errors.
Common Errors
Docker Fails When Building on Apple Silicon
ERROR: exec /usr/local/bin/docker-entrypoint.sh: exec format error
Docker images built with Apple Silicon (or another ARM64 based architecture) can create issues when deploying the images to a Linux or Windows-based AMD64 environment. Before running the docker build command, you must run this command in your terminal:
export DOCKER_DEFAULT_PLATFORM=linux/amd64