How I deployed a Rust web-app using the Rocket framework with Docker

Rust is a systems programming language that runs extremely quickly, prevents segfaults, and guarantees thread safety. Rocket is a web framework for Rust that makes it simple to write fast web applications without sacrificing flexibility or type safety. All with minimal code.

I’ve put together a Dockerfile that helps in the deployment of a Rocket web app, where the setup for the running environment and the serve process have been configured.

Dockerfile
  1. The rustup docker image is used, which is built over debian, and comes with build-essential and the rustup toolchain.
  2. The source files for the Rocket app are placed at src directory. So, if you have an existing Rocket app to deploy, you can replace the contents in src directory with it.
  3. Rocket makes abundant use of Rust’s syntax extensions and other advanced, unstable features. Because of this, a nightly version of Rust is to be used, which is done with the rustup nightly.
  4. Cargo is the Rust package manager. Cargo downloads your Rust project’s dependencies. We build the project using cargo build.
  5. And we run the project with cargo run , which runs the project at 0.0.0.0:8080.

(Shoutout to tcbyrd for providing a detailed explanation on the environmental variables available in Rocket for configuring the PORT and ADDRESS here).

Adding backend features to Rocket

There are plugins like reqwest ,json , with which I made HTTP requests using the Hasura data APIs, to the Postgres database built-in.

HTTP Requests

Comparison of nodeJS-Express and Rust-Rocket Request and Transfer time:

lholznagel in this post has done a detailed comparison on the Request and Transfer time.

Note that the Rocket web-app was built with the — release flag.cargo build --release puts the resulting binary in target/release instead of target/debug. Compilation time is shorter in debug since the compiler doesn’t do optimizations, but the code will run slower. Release mode takes longer to compile, but the code will run faster. You can add the — release flag in the cargo build command in the Dockerfile if you’re planning to use it for production.

I’m currently interning at Hasura. With the Hasura platform, you can deploy anything that can be dockerized, with just a git push.

I’ve put together a Hasura platform quickstart for the Rocket framework.

To try a sample Rust Rocket project on The Hasura platform (it’s free and it’ll only take you a few minutes):

  1. Install the Hasura CLI tool.
  2. Run this command (you can copy the entire section below and run in one shot).
hasura quickstart anirudhm/hello-rust-rocket && 
  cd 

Read more about this quickstart here: https://hasura.io/hub/project/anirudhm/hello-rust-rocket.

I would love to hear your feedback on this, let me know in the comments below :)


Hasura is an open-source engine that gives you realtime GraphQL APIs on new or existing Postgres databases, with built-in support for stitching custom GraphQL APIs and triggering webhooks on database changes.


Hasura

Hasura

The Hasura GraphQL Engine gives you realtime, high performance GraphQL on any Postgres app. Now supports event triggers for use with serverless.

Read More