- Establish Host Directory Structure
Prepare dedicated directories on the host machine to separate configuration artifacts from runtime data. This isolation simplifies backups and permission management.
sudo mkdir -p /opt/cache/redis/{conf,data}
sudo chown -R $(whoami):$(whoami) /opt/cache/redis
- Define Custom Configuration Overrides
Instead of modifying the default template wholesale, inject only the required directives. This approach minimizes drift and ensures compatibility across Redis versions.
# /opt/cache/redis/conf/redis.conf
bind 0.0.0.0 ::1
protected-mode no
requirepass <your_secure_password>
tcp-backlog 511
timeout 0
tcp-keepalive 300
- Launch the Container
Fetch the baseline image and initialize the service. The command below maps network interfaces, attaches persistent volumes, and enforces explicit configuration loading.
docker pull redis:7-alpine
docker run \
--name redis-node \
--restart unless-stopped \
-p 6379:6379 \
--log-opt max-size=50m --log-opt max-file=3 \
-v /opt/cache/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /opt/cache/redis/data:/data \
-d redis:7-alpine \
redis-server /usr/local/etc/redis/redis.conf --appendonly yes
- Runtime Parameter Breakdown
--name redis-node: Assigns a static identifier for predictable reference during exec, logs, or stop/start operations.--restart unless-stopped: Configures the daemon lifecycle policy to automatical restore the container after host reboots or crashes, excluding intentional manual terminations.-p 6379:6379: Exposes the guest port to the host network stack, enabling external client connections.--log-opt max-size/max-file: Enforces log rotation thresholds to prevent container logging drivers from consuming excessive host disk space.-v .../conf/...:/usr/local/etc/redis/redis.conf: Mounts the host-side configuration file into the expected guest path, overriding built-in defaults.-v .../data:/data: Binds the host directory to the container's working volume, guaranteeing dataset survival across container recreation.-d: Operates in detached mode, allowing terminal release while the process continues in the background.redis-server ... --appendonly yes: Forces the startup routine to load the external configuration and activates Append-Only File (AOF) persistence, capturing every mutating command for crash-safe recovery.