Deploy a Rust Web App With Rocket
6 minIntroduction
Rust is a popular programming language offering blazingly fast performance, and guaranteeing memory and thread safety.
In this tutorial, we will explain how to deploy a Rust web application using Rocket, a fast, easy, and flexible web framework. We will then deploy the Rust web application on the Koyeb serverless platform offering a simple way to deploy Rust applications and offering native autoscaling, automatic HTTPS (SSL), auto-healing, and global load-balancing across our edge network with zero configuration.
Requirements
To successfully follow and complete this guide, you need:
- Docker installed on your machine
- A Koyeb account to deploy and run the Rust web application
- The Koyeb CLI installed to interact with Koyeb from the command line
- Have a registry we will use to store our Rust web app Docker image and deploy it on Koyeb
Steps
To successfully deploy a Rust web application on Koyeb Serverless Platform, you need to follow these steps:
- Create a Rust web app using Rocket
- Dockerize the Rust web app
- Push the Docker image to a container registry
- Deploy the Dockerized Rust web app on Koyeb
Create a Rust web app using Rocket
Install Rust
To get started, let's start by configuring a Rust environment we will use to create the application.
If you do not have Rust installed on your machine, you can install using rustup. In your terminal, run the command below:
Once rustup is installed, you may need to restart your current shell to reload your PATH
environment variable. You can avoid this by configuring your current shell running:
To ensure the latest toolchain is properly installed, in your terminal run:
Write the Rust web app using Rocket
To create our application, in your terminal run:
The command above uses Cargo, the Rust package manager to create a new package. The --bin
option indicates to make a binary program.
Cargo created a rust-rocket-app
directory containing two files:
- Cargo.toml: containing the metadata Cargo needs to compile our package
- src/main.rs: a "hello world" program Cargo generated for us
As our web app uses Rocket, we need to add it as a package dependency. In the rust-rocket-app
directory, open the Cargo.toml
file and add rocket as a dependency under the [dependencies]
section.
Open and modify the src/main.rs
with the code below:
The code above creates two routes: /
, returning "Hello, world!" and /hello/<name>
returning "Hello, name!" where "name" is the route parameter. Then it mounts the routes and launches the application.
Finally, create a Rocket.to
To run and test the application locally, run:
If everything goes fine, your app starts and you can test the routes using curl:
Dockerize the Rust web app
To Dockerize our Rust web app, create a Dockerfile
in your project directory.
In this guide, we use Docker multi-stage build to keep the image layers size as small as possible and to ensure our image contains only what is needed to run.
In your Dockerfile, copy the content below:
The first stage is used to build our application, in the second one we copy the application binary from stage one and use it to run the application.
To build the Docker image execute the following command:
In this guide we will push the Docker image to the Docker Hub. You are free to use another different registry as Koyeb allows you to deploy from any container registry.
Once the build is completed, you can run a container using the image locally to validate everything is working as expected running:
If everything goes fine, your container starts properly and you can test the routes using curl:
Push the Docker image to a container registry
With the Docker image built and functional, we can now upload it to the Docker Hub container registry. In your terminal run the command below to push the image:
Once the push command is completed, you will see your Docker image available on the Docker Hub.
Deploy the Dockerized Rust web app on Koyeb
We are now ready to deploy our Rust web application on Koyeb. First, create a Koyeb Secret to store your container registry configuration. In this guide, we will deploy our app from the Docker Hub. For other container registries example, check out the related documentation.
If your Docker image is public, there is no need to create a secret containing your container registry configuration.
We can now deploy the Rust web application on Koyeb Serverless Platform running:
This command creates a new Koyeb App and deploys our Rust application exposing port 8000 and making it publicly accessible on the /
route of your Koyeb App URL.
To retrieve your Koyeb App URL and access your application, run:
Open the URL in your browser to access your application running on Koyeb and natively offering autoscaling, automatic HTTPS (SSL), auto-healing, and global load-balancing across our edge network.