Overview

10 Deploy your application

In this chapter, the application is prepared for real deployment by removing hardcoded settings and making the service adapt to its environment. The code is updated so values like ports, database details, logging levels, and secret keys can come from configuration files, command-line arguments, or environment variables instead of being fixed in the source. This makes the web service easier to run in local development, staging, and production without repeatedly editing code.

The chapter then shows how to build and optimize the Rust binary for production use. Cargo’s build profiles are used to distinguish between development and release builds, with the release profile producing a smaller, faster executable. It also explains cross-compiling for different targets, including Linux binaries built from macOS, and discusses the difference between dynamically and statically linked binaries so the application can be shipped more reliably across systems.

Finally, the chapter expands the build process with a custom build script and Docker-based deployment workflow. A build script generates a version identifier from the current Git commit and exposes it to the program for logging and diagnostics. The application is then packaged into an optimized Docker image, and Docker Compose is used to run the service alongside PostgreSQL in a local multi-container setup, requiring a few environment-driven adjustments such as database credentials and binding the server to all interfaces. By the end, the service is configured to be portable, inspectable, and ready for deployment and maintenance.

Accessing config files and parameters through crates and the standard library
10-01
Cargo uses the dev profile by default, and will optimize the binary if the --release flag is attached.
10-02
Dynamically vs. statically linking in binaries
10-03
A possible workflow for a Docker deployment
10-04

Summary

  • When it comes to deployments, nearly endless possibilities exist to package and deliver your code.
  • To prepare for different scenarios, we can open up our codebase to dynamic changes via environment variables or CLI parameters.
  • We can remove hardcoded strings in our code and read values from config files, .env files, or CLI parameters.
  • The server’s IP address and port, the database URL, and API keys are good candidates for replacing hardcoded strings with outside variables.
  • Once we can dynamically change key parameters in our codebase, we determine which options to use to compile the binary.
  • The package manager Cargo offers various profiles, depending on the goal.
  • A common way of building a Rust binary for production is to add a --release flag to the cargo build command, which triggers a more optimized build profile.
  • We also have the option of targeting different platforms.
  • The Rustup tool supports various targets, which we can add via rustup target add.
  • We then can use cargo build --release --target NAME_OF_TARGET to build for a different operating system than the one we are currently operating on.
  • With the help of a library called musl, we can package the needed system libraries in our binary and generate a statically linked and therefore portable binary.
  • A common way of releasing code is through deploying Docker containers.
  • We can use a multistage Docker container to create smaller images.
  • If we operate in a more complex setup, Docker Compose helps us generate a network of running Docker containers that are networked together.
  • The added config module gives us the chance to remove the reading and parsing of environment variables and config files from the main.rs file and lets us tinker with the logic without interfering with the rest of the code.

FAQ

Why should a Rust web service use environment variables before deployment?Environment variables make the application adaptable to different production setups, where ports, database URLs, logging levels, and API keys may be supplied by a hosting provider instead of being hardcoded.
What are the main ways to provide configuration data to a Rust application?The chapter highlights three common approaches: config files, command-line inputs, and dotenv (.env) files in the project root.
How do config files help when deploying a web application?Config files make startup settings easier to understand and reproduce because you can inspect a JSON or TOML file instead of remembering long CLI commands or searching through source code.
Why is the config crate used in this chapter?The config crate is used to read configuration files, such as TOML, and deserialize them into a Rust struct so the application can use those values directly.
How does the chapter read values from a setup.toml file?It creates a Config builder, adds the setup file as a source, builds the configuration, and then deserializes it into an Args struct.
Why use clap for command-line arguments?clap provides a convenient way to parse CLI parameters into a struct, generate help text automatically, and define defaults, short flags, and long flags.
What is the purpose of the .env file in this chapter?The .env file stores environment variables locally, especially secrets like API keys and encryption keys, and is meant to be excluded from version control.
Why does the application check for missing environment variables at startup?It fails early if required secrets are missing, preventing the server from starting in a broken or insecure state.
What is the benefit of building with cargo build --release?The release profile produces a smaller and more optimized binary, which is generally better for production deployments.
Why are Docker and Docker Compose used in this deployment chapter?Docker helps package the application into a container, and Docker Compose makes it easier to run the web service together with PostgreSQL in a local multi-container environment.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Rust Web Development Rust Web Development ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Rust Web Development Rust Web Development ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Rust Web Development Rust Web Development ebook for free