Docker 和 Compose 安装 Redis
Dockerfile 介绍
官方镜像:https://hub.docker.com/_/redis/
参考博客:https://zhuanlan.zhihu.com/p/62957397
Docker 启动命令
shell
docker run --name redis --restart=unless-stopped -p 6379:6379 -d redis:6 --requirepass 123456Docker Compose 启动文件
yaml
services:
redis:
image: redis:8.6.2
container_name: redis
restart: unless-stopped
ports:
- "6379:6379"
environment:
- TZ=Asia/Shanghai
command: redis-server --requirepass 1qaz2wsx
healthcheck:
test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
interval: 30s
timeout: 5s
retries: 3
start_period: 10scommand:
--requirepass 1qaz2wsx: 设置密码
healthcheck:
test: ["CMD", "redis-cli", "--raw", "incr", "ping"]: 检测 Redis 是否正常启动。interval: 30s: 检测间隔timeout: 10s: 超时时间retries: 3: 检测失败重试次数start_period: 10s: 启动时等待 10 秒后再开始检查。
Docker 连接、查看 redis 容器
shell
docker exec -it redis redis-cli
docker exec -it redis redis-cli -h 127.0.0.1 -p 6379 -a 123456
docker exec -it “容器ID” redis-cli
docker exec -it “容器ID” redis-cli -h “内网” –a “redis密码”
docker exec -it 34aee08bf67d redis-cli
docker exec -it 34aee08bf67d redis-cli -h 127.0.0.1 -p 6379 -a 123456Redis (error) NOAUTH Authentication required.解决方法
shell
auth 密码
set hello jack
get helloredis 常见内置命令
| 步骤 | 操作/命令 | 说明 |
|---|---|---|
| 1 | redis-cli | 连接到Redis服务器。 |
| 2 | info keyspace | 查看所有存在数据的数据库列表及其键数量。 |
| 3 | select <db_index> | 切换到指定的数据库。 |
| 4 | dbsize | 查看当前数据库的键总数。 |
| 5 | keys * | 列出当前数据库的所有键(谨慎使用)。 |
ERR unknown command 'JSON.SET'
有时候我们会遇到这个问题,这是由于 redis 8 版本之前,默认的官方镜像没有加载 JSON 模块导致的。
此时,我们就不能用最原始的镜像了,而需要用这个镜像:https://hub.docker.com/r/redis/redis-stack
或者使用 redis 8 及以后的版本。
shell
docker run -d --name redis-stack --restart=unless-stopped \
-p 6379:6379 -p 8001:8001 \
-e REDIS_ARGS="--requirepass 1qaz2wsx" \
redis/redis-stack:7.2.0-v1-x86_64这下就可以使用 JSON.SET 命令了
shell
127.0.0.1:6379> JSON.SET object . '{"foo": "bar", "ans": 42}'
OK
127.0.0.1:6379> JSON.GET object
"{\"foo\":\"bar",\"ans\":42}"更多说明参考 docker hub redis-stack 镜像说明。
注意:旧的 CPU 微架构 x86-64-v1 只能使用低版本的,比如:redis/redis-stack:6.2.6-v19
