docs/examples/data/redis.mdx
<Tooltip tip="This example publishes Redis to a port on the computer running the CLI, which is not available on microsandbox cloud."><span className="msb-badge-local">Local-only <Icon icon="circle-info" size={11} /></span></Tooltip>
This example starts Redis 7 with password authentication, a persistent named volume, and a localhost-only forwarded port.
Set a temporary password in the host shell:
<CodeGroup> ```sh macOS & Linux export REDIS_PASSWORD="$(openssl rand -hex 24)" ```$bytes = New-Object byte[] 24
$rng = [Security.Cryptography.RandomNumberGenerator]::Create()
$rng.GetBytes($bytes)
$rng.Dispose()
$env:REDIS_PASSWORD = -join ($bytes | ForEach-Object { $_.ToString('x2') })
Start Redis:
<CodeGroup> ```sh macOS & Linux msb run -d --name redis-demo --replace \ --cpus 1 --memory 512M --root-disk 1G \ -p 127.0.0.1:56379:6379 \ --mount-named redis-data:/data \ redis:7-alpine -- redis-server \ --bind 0.0.0.0 \ --protected-mode yes \ --appendonly yes \ --requirepass "$REDIS_PASSWORD" ```msb run -d --name redis-demo --replace `
--cpus 1 --memory 512M --root-disk 1G `
-p 127.0.0.1:56379:6379 `
--mount-named redis-data:/data `
redis:7-alpine -- redis-server `
--bind 0.0.0.0 `
--protected-mode yes `
--appendonly yes `
--requirepass $env:REDIS_PASSWORD
Write a test value:
<CodeGroup> ```sh macOS & Linux msb exec redis-demo -- \ redis-cli -a "$REDIS_PASSWORD" --no-auth-warning SET example ready ```msb exec redis-demo -- `
redis-cli -a $env:REDIS_PASSWORD --no-auth-warning SET example ready
Read it back:
<CodeGroup> ```sh macOS & Linux msb exec redis-demo -- \ redis-cli -a "$REDIS_PASSWORD" --no-auth-warning GET example ```msb exec redis-demo -- `
redis-cli -a $env:REDIS_PASSWORD --no-auth-warning GET example
Finally, check that the server responds:
<CodeGroup> ```sh macOS & Linux msb exec redis-demo -- \ redis-cli -a "$REDIS_PASSWORD" --no-auth-warning PING ```msb exec redis-demo -- `
redis-cli -a $env:REDIS_PASSWORD --no-auth-warning PING
The three commands should return OK, ready, and PONG. Host applications connect to 127.0.0.1:56379.
Remove the sandbox while retaining the append-only database:
msb rm -f redis-demo
Delete all persisted Redis data:
msb volume rm redis-data
Clear the password from the host shell:
<CodeGroup> ```sh macOS & Linux unset REDIS_PASSWORD ```Remove-Item Env:REDIS_PASSWORD