pub struct RedisTemplate { /* private fields */ }Expand description
Redis container template with sensible defaults
Implementations§
Source§impl RedisTemplate
impl RedisTemplate
Sourcepub fn with_persistence(self, volume_name: impl Into<String>) -> Self
pub fn with_persistence(self, volume_name: impl Into<String>) -> Self
Enable persistence with a volume
Sourcepub fn config_file(self, config_path: impl Into<String>) -> Self
pub fn config_file(self, config_path: impl Into<String>) -> Self
Set custom Redis configuration file
Sourcepub fn memory_limit(self, limit: impl Into<String>) -> Self
pub fn memory_limit(self, limit: impl Into<String>) -> Self
Set memory limit for Redis
Sourcepub fn cluster_mode(self) -> Self
pub fn cluster_mode(self) -> Self
Enable Redis cluster mode
Sourcepub fn maxmemory_policy(self, policy: impl Into<String>) -> Self
pub fn maxmemory_policy(self, policy: impl Into<String>) -> Self
Set max memory policy
Sourcepub fn network_mode(self, mode: impl Into<String>) -> Self
pub fn network_mode(self, mode: impl Into<String>) -> Self
Set the container network mode (e.g. "host", "bridge", "none").
This is a thin alias over network that reads better
when selecting a Docker network mode rather than a named network. See
host_network for the host-mode caveats.
Sourcepub fn host_network(self) -> Self
pub fn host_network(self) -> Self
Run the container with --network host.
In host networking mode the container shares the host’s network
namespace: the Redis port is reachable directly on the host with no
published port mapping, so no -p flag is emitted and the host-side
port equals the container port (6379 by default).
§Platform support
Host networking is a Linux-only Docker feature. On Docker Desktop
for macOS and Windows the daemon runs inside a Linux VM, so
--network host binds ports inside that VM rather than on your machine:
the option is effectively a no-op there and the Redis port will not
be reachable from the host. This method does not return an error on
non-Linux hosts (the Docker CLI accepts the flag regardless of backend);
only use host mode against a native Linux daemon.
§Example
use docker_wrapper::template::{RedisTemplate, Template};
use docker_wrapper::DockerCommand;
// Linux only: Redis is reachable on localhost:6379 with no -p mapping.
let template = RedisTemplate::new("host-redis").host_network();
let args = template.build_command().build_command_args();
assert!(args.contains(&"--network".to_string()));
assert!(args.contains(&"host".to_string()));
// Host mode publishes no ports.
assert!(!args.contains(&"--publish".to_string()));Sourcepub fn auto_remove(self) -> Self
pub fn auto_remove(self) -> Self
Enable auto-remove when stopped
Sourcepub fn with_redis_stack(self) -> Self
pub fn with_redis_stack(self) -> Self
Use Redis Stack image instead of basic Redis.
Uses the redis/redis-stack image pinned to a known-good default tag
(7.4.0-v3) rather than latest, so that runs are reproducible. Call
stack_version to pin a different tag.
Sourcepub fn stack_version(self, tag: impl Into<String>) -> Self
pub fn stack_version(self, tag: impl Into<String>) -> Self
Pin the Redis Stack image tag (e.g. "7.4.0-v3").
Only affects the image used when Self::with_redis_stack is enabled.
The default is a known-good pinned tag rather than latest, so that runs
are reproducible. For full control over both the image name and tag, use
Self::custom_image instead.
§Example
use docker_wrapper::template::RedisTemplate;
let template = RedisTemplate::new("my-redis")
.with_redis_stack()
.stack_version("7.4.0-v3");Sourcepub fn custom_image(
self,
image: impl Into<String>,
tag: impl Into<String>,
) -> Self
pub fn custom_image( self, image: impl Into<String>, tag: impl Into<String>, ) -> Self
Use a custom image and tag
Sourcepub fn platform(self, platform: impl Into<String>) -> Self
pub fn platform(self, platform: impl Into<String>) -> Self
Set the platform for the container (e.g., “linux/arm64”, “linux/amd64”)
Sourcepub fn tls(self, certs_dir: impl Into<String>) -> Self
pub fn tls(self, certs_dir: impl Into<String>) -> Self
Enable TLS, bind-mounting the given host certificate directory.
The directory is mounted read-only into the container and Redis is
started with --tls-port, --tls-cert-file, --tls-key-file and
--tls-ca-cert-file. The directory must contain these files:
redis.crt– the server certificateredis.key– the server private keyca.crt– the CA certificate used to verify client certificates
By default the plaintext port stays open alongside TLS; call
tls_only to disable plaintext (--port 0). The TLS
port is published on the host (6380 by default, override with
tls_port).
§Generating throwaway certificates
For local testing you can generate a self-signed CA and server
certificate with openssl:
openssl genrsa -out ca.key 2048
openssl req -x509 -new -nodes -key ca.key -sha256 -days 365 \
-subj "/CN=test-ca" -out ca.crt
openssl genrsa -out redis.key 2048
openssl req -new -key redis.key -subj "/CN=localhost" -out redis.csr
openssl x509 -req -in redis.csr -CA ca.crt -CAkey ca.key \
-CAcreateserial -days 365 -sha256 -out redis.crt§Example
use docker_wrapper::template::{RedisTemplate, Template};
use docker_wrapper::DockerCommand;
let template = RedisTemplate::new("tls-redis").tls("/path/to/certs");
let args = template.build_command().build_command_args();
assert!(args.contains(&"--tls-port".to_string()));
assert!(args.contains(&"6380".to_string()));Sourcepub fn tls_port(self, port: u16) -> Self
pub fn tls_port(self, port: u16) -> Self
Set the host-side port published for TLS connections (default 6380).
Only has an effect when TLS is enabled via tls.
Sourcepub fn tls_only(self) -> Self
pub fn tls_only(self) -> Self
Disable the plaintext port and serve only TLS.
Sets --port 0 (which tells Redis to stop listening on the plaintext
port) and skips publishing the plaintext port mapping. Only has an
effect when TLS is enabled via tls.
Sourcepub fn tls_connection_string(&self) -> Option<String>
pub fn tls_connection_string(&self) -> Option<String>
Returns the TLS connection string in URL format, or None when TLS is
not enabled.
Format: rediss://[:password@]host:port, where port is the published
TLS port (6380 by default, see tls_port).
§Example
use docker_wrapper::template::RedisTemplate;
let template = RedisTemplate::new("my-redis").tls("/certs");
assert_eq!(
template.tls_connection_string().as_deref(),
Some("rediss://localhost:6380")
);
let plaintext = RedisTemplate::new("my-redis");
assert_eq!(plaintext.tls_connection_string(), None);Trait Implementations§
Source§impl HasConnectionString for RedisTemplate
impl HasConnectionString for RedisTemplate
Source§fn connection_string(&self) -> String
fn connection_string(&self) -> String
Returns the Redis connection string in URL format.
Format: redis://[:password@]host:port
When the template is configured for TLS-only access (see
tls_only) the plaintext port is disabled, so
this returns the rediss:// TLS endpoint instead. When TLS is enabled
alongside plaintext, this still returns the plaintext URL; use
tls_connection_string for the
TLS endpoint.
§Example
use docker_wrapper::template::{RedisTemplate, HasConnectionString};
let template = RedisTemplate::new("my-redis").port(6380);
assert_eq!(template.connection_string(), "redis://localhost:6380");
let template_with_pass = RedisTemplate::new("my-redis")
.port(6380)
.password("secret");
assert_eq!(template_with_pass.connection_string(), "redis://:secret@localhost:6380");
// TLS-only falls back to the rediss:// endpoint.
let tls = RedisTemplate::new("my-redis").tls("/certs").tls_only();
assert_eq!(tls.connection_string(), "rediss://localhost:6380");