Expand description
§docker-env
docker-env
is a minimal utility crate for reading environment variables
and Docker-style secrets in containerised Rust applications.
This crate is designed for use in Docker, Kubernetes, and other container environments where secrets may be injected as file-based mounts rather than plain environment variables.
§Features
- Typed access to environment variables (
get_env<T>
,get_env_or
, etc.). - Support for secret files via the
_FILE
suffix convention. - Graceful fallbacks with optional, default, or required values.
- Logs errors using the
tracing
crate.
§Docker Secret Convention
When a variable has a secret counterpart (e.g., DATABASE_PASSWORD
),
Docker often mounts the secret as a file and sets an accompanying
environment variable with the _FILE
suffix:
DATABASE_PASSWORD_FILE=/run/secrets/db_password
Calling:
let password: String = docker_env::get_env("DATABASE_PASSWORD", true).unwrap();
will cause docker-env
to read the contents of the file at
/run/secrets/db_password
instead of the plain DATABASE_PASSWORD
variable.
§Example
use docker_env::{get_env, get_env_or, get_env_or_panic};
let db_url: String = get_env_or_panic("DATABASE_URL", false);
let port: u16 = get_env_or("PORT", 8080, false);
let api_key: String = get_env("API_KEY", true).unwrap();
§When to Use
Use this crate when your app:
- Runs in a Docker or Kubernetes environment
- Uses file-based secrets via
/run/secrets/
or similar - Requires simple, typed configuration with error logging
Modules§
- prelude
- Commonly used imports for convenience.
Functions§
- get_env
- Reads an environment variable.
- get_
env_ or - Reads an environment variable and returns its value if it exists; otherwise,
the
default
value is returned. - get_
env_ or_ default - Reads an environment variable and returns its value if it exists; otherwise, the default value is returned.
- get_
env_ or_ panic - Reads an environment variable and returns its value if it exists; otherwise, this function will panic.
- get_
secret - Reads an environment variable containing the path to a Docker secret file.