Chapter 10: Deploy your application
The introduction explains why deployment requires externally supplied configuration and previews production build and packaging topics.
After this chapter you will be able to
- External deployment configuration. Explain why hardcoded application settings should be supplied externally when deploying to a hosting provider, and give examples of those settings.
- Deployment configuration needs. Explain why a compiled web service needs externally supplied configuration across environments.
- Config file tradeoffs. Describe the convenience of config files and the limitation of storing secrets in them.
- Command-line input tradeoffs. Explain how command-line inputs help configure changing environments and what makes them harder to follow.
- Dotenv files and secrets. Describe how dotenv files supply environment-specific secrets and why an example file is useful.
- TOML configuration setup. Describe how the example uses config-rs and setup.toml to configure the web service.
- Configuration separation benefits. Explain why keeping settings in a configuration file helps maintainers and contributors.
- Parsing arguments with clap. Describe the options for reading command-line arguments and how the example uses clap with the existing Args struct.
- Passing arguments at startup. Describe how to pass application arguments when running with Cargo or with the compiled binary.
- Environment Variable Configuration. Explain how dotenv makes configuration available to the web service and how hosting-provider settings can affect the server.
- Secret Key Configuration. Describe how the section moves secret keys into environment configuration and keeps the .env file out of the repository.
- API Key Validation. Explain the startup check for the third-party API key and its limitation when the value is empty.
- Cargo build profiles. Explain which profile Cargo uses by default and how to request a release build.
- Cross compilation motivation. Explain why a binary built locally may need cross-compilation for a different production environment.
- Cargo build profile selection. Explain which Cargo profile is used by default, how to select the release profile for production, and where profile settings can be customized.
- Optimization level tradeoffs. Explain how opt-level values relate to compilation speed, optimization, and binary performance, and why the best setting may need experimentation.
- Compilation targets and setup. Explain why a binary built for one environment may not run in another and describe how to prepare a different Rust compilation target.
- Static and dynamic linking. Distinguish dynamically linked from statically linked binaries in terms of included libraries, binary size, and operating-system dependencies.
- Musl Linux build requirements. Describe how the musl target supports a cross-compiled Linux binary and what the build machine needs to provide.
- Cargo build script purpose. Explain when Cargo runs build.rs and why it is useful for release-dependent work.
- Build version environment variable. Trace how the example generates a build version, makes it available to the application, and records it at startup.
- Docker packaging and usage. Explain how Docker containers fit into packaging and using the Rust web service.
- Static Linking for Containers. Explain why the section uses a statically linked binary for its Docker container.
- Musl Container Build Sequence. Describe the Dockerfile steps used to prepare, build, and run the statically linked binary, including the target and build profile.
- Local Compose environment. Describe how Docker Compose creates a local environment for the web service and PostgreSQL, and how to check it after rebuilding.
- Container connection adjustments. Explain the database connection and web server address changes required for the container environment.
- Configuration precedence and secrets. Explain the order in which the application obtains configuration values and how the section treats credentials.
- Configuration module extraction. Explain how extracting configuration into a config module simplifies main.rs and makes available settings easier to find.
- Local PostgreSQL credentials. Describe how to prepare a PostgreSQL user and database credentials for running the server without Docker.
- Environment settings and secrets. Explain how environment-specific variables and a private .env file keep settings and secrets out of the codebase.
What it builds on
- Integrating tracing in our application (chapter 6) · Using Tracing in the application
- Setting up our example database (chapter 7) · The example database setup.
Section by section
Introduction
The introduction explains why deployment requires externally supplied configuration and previews production build and packaging topics.
- External deployment configuration: Explain why hardcoded application settings should be supplied externally when deploying to a hosting provider, and give examples of those settings.
Why it matters: External configuration lets your application adapt to the environment where it is deployed.
Key terms: environment variables, CLI commands, hardcoded, database URL
10.1 Setting up your application through environment variables
This section explains why a deployable binary needs external configuration and compares config files, command-line inputs, and dotenv files.
- Deployment configuration needs: Explain why a compiled web service needs externally supplied configuration across environments.
Why it matters: External configuration lets the same application adapt to where it runs. - Config file tradeoffs: Describe the convenience of config files and the limitation of storing secrets in them.
Why it matters: Config files make settings visible, but they are not a safe reason to commit production secrets. - Command-line input tradeoffs: Explain how command-line inputs help configure changing environments and what makes them harder to follow.
Why it matters: CLI inputs make environment-specific startup flexible, but their values may not be apparent in the code. - Dotenv files and secrets: Describe how dotenv files supply environment-specific secrets and why an example file is useful.
Why it matters: Separate dotenv files can supply the right secrets without sharing the actual secret values.
Key terms: binary, database port, logging levels, API keys, config files, JSON, TOML, secrets, command-line inputs, CLI inputs
10.1.1 Set up config files
This section shows how to move web service configuration into a TOML file using config-rs and explains the benefits of separating settings from code.
- TOML configuration setup: Describe how the example uses config-rs and setup.toml to configure the web service.
Why it matters: This setup gives the service a separate place to specify settings it needs at startup. - Configuration separation benefits: Explain why keeping settings in a configuration file helps maintainers and contributors.
Why it matters: A separate configuration file makes routine setting changes safer and local setup easier to understand.
Key terms: config-rs, toml feature flag, setup.toml, main.rs, parameters, source code, local setup
10.1.2 Accept command-line inputs for your application
This section explains how command-line arguments provide flexible application configuration and how the example parses them with clap.
- Command-line configuration flexibility: Explain why command-line parameters can be useful for configuring a deployed application.
Why it matters: This gives you a flexible alternative when a complex configuration file is unnecessary. - Parsing arguments with clap: Describe the options for reading command-line arguments and how the example uses clap with the existing Args struct.
Why it matters: This connects the arguments supplied at startup to a structured configuration object in the application. - Passing arguments at startup: Describe how to pass application arguments when running with Cargo or with the compiled binary.
Why it matters: Knowing where to put the arguments lets you actually supply the configuration at startup.
Key terms: CLI parameters, config file, std::env::args, clap, Args, Cargo.toml, cargo run, compiled binary, arguments
10.1.3 Read and parse environment variables into your web service
This section configures the web service with environment variables loaded from a .env file and discusses handling secrets and missing values.
- Environment Variable Configuration: Explain how dotenv makes configuration available to the web service and how hosting-provider settings can affect the server.
Why it matters: Environment-based configuration lets the same service adapt to its hosting environment. - Secret Key Configuration: Describe how the section moves secret keys into environment configuration and keeps the .env file out of the repository.
Why it matters: Externalizing keys avoids hardcoding them and helps keep the secrets file out of the repository. - API Key Validation: Explain the startup check for the third-party API key and its limitation when the value is empty.
Why it matters: An early check catches missing configuration, but an empty key can still escape a presence-only check.
Key terms: environment variables, dotenv, .env, port, host IP, .gitignore, paseto encryption key, BAD_WORDS_API_KEY, profanity.rs, environment variable
10.2 Compiling your web service for different environments
This section introduces Cargo build profiles and why cross-compilation matters when development and production environments differ.
- Cargo build profiles: Explain which profile Cargo uses by default and how to request a release build.
Why it matters: This distinction lets you choose between a default development build and a release build. - Cross compilation motivation: Explain why a binary built locally may need cross-compilation for a different production environment.
Why it matters: A successful local build does not by itself ensure that the binary will run in production.
Key terms: Cargo, dev profile, release profile, cargo build, --release, cross-compilation, binary, operating system, production
10.2.1 Development vs. release flag when building your binary
This section compares Cargo’s development and release build profiles and explains how optimization settings affect compilation and the resulting binary.
- Cargo build profile selection: Explain which Cargo profile is used by default, how to select the release profile for production, and where profile settings can be customized.
Why it matters: Choosing the appropriate build profile helps prepare your service for production. - Optimization level tradeoffs: Explain how opt-level values relate to compilation speed, optimization, and binary performance, and why the best setting may need experimentation.
Why it matters: Understanding the tradeoff helps you choose and evaluate build settings for your service.
Key terms: cargo build, dev profile, release profile, --release, Cargo.toml, opt-level, optimization, compile times, binary size
10.2.2 Cross-compile your binary for different environments
This section explains Rust compilation targets, static versus dynamic linking, and using musl to build a Linux binary.
- Compilation targets and setup: Explain why a binary built for one environment may not run in another and describe how to prepare a different Rust compilation target.
Why it matters: Choosing the right target helps you produce a binary for the environment where it will run. - Static and dynamic linking: Distinguish dynamically linked from statically linked binaries in terms of included libraries, binary size, and operating-system dependencies.
Why it matters: The linking choice affects both binary size and reliance on the deployment environment. - Musl Linux build requirements: Describe how the musl target supports a cross-compiled Linux binary and what the build machine needs to provide.
Why it matters: A musl target and its build requirements help you prepare a statically linked Linux binary.
Key terms: targets, rustup target list, Rustup, dynamically linked, statically linked, libraries, musl, x86_64-unknown-linux-musl, standard C library
10.3 Using build.rs in your build process
This section explains how to use a Cargo build script to generate a build version and log it when the application starts.
- Cargo build script purpose: Explain when Cargo runs build.rs and why it is useful for release-dependent work.
Why it matters: A build script gives you a Rust-based place for work that belongs to the build rather than the application’s business logic. - Build version environment variable: Trace how the example generates a build version, makes it available to the application, and records it at startup.
Why it matters: A logged build version helps connect a problem to the specific commit and build being run. - Startup log visibility: Explain why the version log initially does not appear and how the example makes it visible.
Why it matters: Knowing how the log level affects startup output helps you find the build version without changing code.
Key terms: build.rs, Cargo, business logic, cargo:rustc-env, platforms, build-dependencies, env!, Tracing, commit hash, log level
10.4 Creating the right Docker image for your web service
This section introduces Docker containers as a way to package the Rust web service for local development and deployment.
- Docker packaging and usage: Explain how Docker containers fit into packaging and using the Rust web service.
Why it matters: This connects the way you package the service with both development and deployment.
Key terms: Docker container, local development environment, deployment pipeline
10.4.1 Create a statically linked Docker image
This section explains why and how to build a statically linked Rust binary for a Docker container using a musl target.
- Static Linking for Containers: Explain why the section uses a statically linked binary for its Docker container.
Why it matters: This explains why the deployment uses a statically linked binary rather than requiring a more fully equipped image. - Musl Container Build Sequence: Describe the Dockerfile steps used to prepare, build, and run the statically linked binary, including the target and build profile.
Why it matters: These steps turn the cross-compilation approach into a container build that produces and runs the intended binary. - Docker Image Foundations: Describe what a Docker image provides for a container and identify the Rust image mentioned in the section.
Why it matters: The image supplies the foundation on which the application container runs.
Key terms: statically linked, self-contained binary, Docker image, musl target, Rustup, statically link, Cargo, release profile, cross-compilation, Docker container
10.4.2 Set up a local Docker environment with docker-compose
This section sets up a local Docker Compose environment for the web service and PostgreSQL, adjusts container configuration, and checks the resulting service.
- Local Compose environment: Describe how Docker Compose creates a local environment for the web service and PostgreSQL, and how to check it after rebuilding.
Why it matters: A connected local environment lets you test the service against its database before deployment. - Container connection adjustments: Explain the database connection and web server address changes required for the container environment.
Why it matters: Correct addresses and credentials let the service reach its database and accept external requests. - Configuration precedence and secrets: Explain the order in which the application obtains configuration values and how the section treats credentials.
Why it matters: Configuration precedence makes the service flexible while avoiding hard-coded credentials.
Key terms: Docker Compose, docker-compose.yml, docker-compose up, curl, connection URL, localhost, 127.0.0.1, 0.0.0.0, PostgreSQL, .env
10.4.3 Extract the configuration of the web server into a new module
The section extracts server configuration into a module and prepares environment-specific settings for running the service with or without Docker.
- Configuration module extraction: Explain how extracting configuration into a config module simplifies main.rs and makes available settings easier to find.
Why it matters: Separating configuration makes the startup code easier to read and the available settings easier to inspect. - Local PostgreSQL credentials: Describe how to prepare a PostgreSQL user and database credentials for running the server without Docker.
Why it matters: Local database credentials let the same web server run outside its Docker environment. - Environment settings and secrets: Explain how environment-specific variables and a private .env file keep settings and secrets out of the codebase.
Why it matters: Environment-specific settings allow different deployments while keeping these secrets out of the repository.
Key terms: config module, main.rs, configuration parameters, PostgreSQL, PSQL, .env, rustwebdev, environment variables, .gitignore, API keys
10.5 Summary
Where this is used later
- Testing the Config module with environment variables (chapter 11) · Tests configuration that uses environment variables.
- Testing the profanity module with a newly created mock server (chapter 11) · Using environment variables to configure the web service.
Concepts explained in this chapter
- cross-compilationCross-compilation is the process of building executable software for a target environment different from the one where the build is performed.
- .env fileAn .env file is a text file containing environment-variable definitions that applications or tools can load as configuration.
- environment variableAn environment variable is a named value supplied by a process's environment that programs can read to configure behavior.
- configuration fileA configuration file contains settings that software reads to determine its behavior.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
