背景简介
基于 Docker 部署 redis。
前置信息
- Docker
详细信息
文件架构
.
├── data
│ ├── redis.conf
│ └── redis-data
└── docker-compose.yml
配置文件
- 准备:
docker-compose.yml
services:
redis-dev:
image: redis:7-alpine
container_name: redis-dev
restart: unless-stopped
ports:
- "6379:6379"
volumes:
# 挂载配置文件
- ./data/redis.conf:/usr/local/etc/redis/redis.conf
# 挂载数据目录
- ./data/redis-data:/data
command: redis-server /usr/local/etc/redis/redis.conf
- 准备:
data/redis.conf
# 允许任何IP连接,因为我们在Docker网络中
# 如果只希望从容器内部访问,可以设置为 127.0.0.1
bind 0.0.0.0
# 端口
port 6379
# 设置密码,请替换成一个强密码
requirepass your_very_strong_password
# 开启 AOF 持久化
appendonly yes
appendfsync everysec
# RDB 持久化策略 (可选,AOF 通常更安全)
# save 900 1
# save 300 10
# save 60 10000
部署
- 部署容器
docker compose up -d
[+] Running 2/2
✔ Network redis_default Created
✔ Container redis-dev Started
1:C 28 Oct 2025 07:45:14.679 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 28 Oct 2025 07:45:14.679 * Redis version=7.4.6, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 28 Oct 2025 07:45:14.679 * Configuration loaded
1:M 28 Oct 2025 07:45:14.680 * monotonic clock: POSIX clock_gettime
1:M 28 Oct 2025 07:45:14.681 * Running mode=standalone, port=6379.
1:M 28 Oct 2025 07:45:14.681 * Server initialized
1:M 28 Oct 2025 07:45:14.687 * Creating AOF base file appendonly.aof.1.base.rdb on server start
1:M 28 Oct 2025 07:45:14.699 * Creating AOF incr file appendonly.aof.1.incr.aof on server start
1:M 28 Oct 2025 07:45:14.699 * Ready to accept connections tcp
验证部署
- 连接 Redis
docker exec -it redis-dev redis-cli
127.0.0.1:6379>
- 认证密码
AUTH your_very_strong_password
OK
127.0.0.1:6379>
- 设置键值对
SET mykey "Hello, Redis 7!"
OK
127.0.0.1:6379>
- 获取键值
GET mykey
"Hello, Redis 7!"
127.0.0.1:6379>
以上便是本文的全部内容,感谢您的阅读,如遇到任何问题,欢迎在评论区留言讨论。