Skip to main content

RedisTemplate

Struct RedisTemplate 

Source
pub struct RedisTemplate { /* private fields */ }
Expand description

Redis container template with sensible defaults

Implementations§

Source§

impl RedisTemplate

Source

pub fn new(name: impl Into<String>) -> Self

Create a new Redis template with default settings

Source

pub fn port(self, port: u16) -> Self

Set a custom Redis port

Source

pub fn password(self, password: impl Into<String>) -> Self

Set Redis password

Source

pub fn with_persistence(self, volume_name: impl Into<String>) -> Self

Enable persistence with a volume

Source

pub fn config_file(self, config_path: impl Into<String>) -> Self

Set custom Redis configuration file

Source

pub fn memory_limit(self, limit: impl Into<String>) -> Self

Set memory limit for Redis

Source

pub fn cluster_mode(self) -> Self

Enable Redis cluster mode

Source

pub fn maxmemory_policy(self, policy: impl Into<String>) -> Self

Set max memory policy

Source

pub fn version(self, version: impl Into<String>) -> Self

Use a specific Redis version

Source

pub fn network(self, network: impl Into<String>) -> Self

Connect to a specific network

Source

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.

Source

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()));
Source

pub fn auto_remove(self) -> Self

Enable auto-remove when stopped

Source

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.

Source

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");
Source

pub fn custom_image( self, image: impl Into<String>, tag: impl Into<String>, ) -> Self

Use a custom image and tag

Source

pub fn platform(self, platform: impl Into<String>) -> Self

Set the platform for the container (e.g., “linux/arm64”, “linux/amd64”)

Source

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 certificate
  • redis.key – the server private key
  • ca.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()));
Source

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.

Source

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.

Source

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

Source§

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");
Source§

impl Template for RedisTemplate

Source§

fn name(&self) -> &str

Get the template name
Source§

fn config(&self) -> &TemplateConfig

Get the template configuration
Source§

fn config_mut(&mut self) -> &mut TemplateConfig

Get a mutable reference to the configuration
Source§

fn wait_for_ready<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Wait for the container to be ready Read more
Source§

fn build_command(&self) -> RunCommand

Build the RunCommand for this template
Source§

fn start<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Start the container with this template
Source§

fn start_and_wait<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Start the container and wait for it to be ready
Source§

fn stop<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Stop the container
Source§

fn remove<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Remove the container
Source§

fn is_running<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Check if the container is running
Source§

fn logs<'life0, 'life1, 'async_trait>( &'life0 self, follow: bool, tail: Option<&'life1 str>, ) -> Pin<Box<dyn Future<Output = Result<CommandOutput>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Get container logs
Source§

fn exec<'life0, 'life1, 'async_trait>( &'life0 self, command: Vec<&'life1 str>, ) -> Pin<Box<dyn Future<Output = Result<ExecOutput>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Execute a command in the running container

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more