当前位置: 首页 > article >正文

使用 Docker Compose 安装 Redis 7.2.4

前面是指南,后面是主要步骤实际执行日志

使用 Docker Compose 安装 Redis 7.2.4

以下是使用 Docker Compose 安装 Redis 7.2.4 的完整指南:

1. 创建项目目录和文件

bash

复制

下载

# 创建项目目录
mkdir redis-docker && cd redis-docker# 创建必要目录
mkdir -p data conf

2. 创建 docker-compose.yml 文件

yaml

复制

下载

# docker-compose.yml
version: '3.8'services:redis:image: redis:7.2.4container_name: redis-serverrestart: unless-stoppedports:- "6379:6379"volumes:- ./data:/data- ./conf/redis.conf:/usr/local/etc/redis/redis.confcommand: redis-server /usr/local/etc/redis/redis.confenvironment:- TZ=Asia/Shanghainetworks:- redis-netnetworks:redis-net:driver: bridge

3. 创建 Redis 配置文件

bash

复制

下载

# 创建配置文件
cat <<EOF > conf/redis.conf
# Redis 7.2.4 基础配置bind 0.0.0.0
port 6379
protected-mode yes
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo no# 持久化配置
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes# 内存管理
maxmemory 1gb
maxmemory-policy allkeys-lru
maxmemory-samples 5# 安全设置
# requirepass yourstrongpassword# 高级设置
timeout 0
tcp-keepalive 300
repl-backlog-size 1mb
repl-backlog-ttl 3600
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no# 性能优化
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60# 集群模式 (单节点不需要)
# cluster-enabled no
# cluster-config-file nodes.conf
# cluster-node-timeout 15000
# cluster-replica-validity-factor 10
# cluster-migration-barrier 1
# cluster-require-full-coverage yes
EOF

4. 启动 Redis 服务

bash

复制

下载

# 启动服务
docker-compose up -d# 查看服务状态
docker-compose ps# 预期输出:
#     Name                   Command               State           Ports
# ----------------------------------------------------------------------------
# redis-server   docker-entrypoint.sh redis ...   Up      0.0.0.0:6379->6379/tcp

5. 验证安装

bash

复制

下载

# 测试 Redis 连接
docker-compose exec redis redis-cli ping
# 应返回: PONG# 查看 Redis 信息
docker-compose exec redis redis-cli info server# 检查持久化
docker-compose exec redis redis-cli save
ls -lh data/

6. 基本操作和管理

进入 Redis CLI

bash

复制

下载

docker-compose exec redis redis-cli

常用命令

bash

复制

下载

# 设置键值
SET mykey "Hello Redis"# 获取键值
GET mykey# 查看所有键
KEYS *# 查看内存使用
INFO memory# 查看客户端连接
CLIENT LIST

管理服务

bash

复制

下载

# 停止服务
docker-compose down# 启动服务
docker-compose up -d# 重启服务
docker-compose restart# 查看日志
docker-compose logs -f

7. 自定义配置

要修改 Redis 配置:

  1. 编辑 conf/redis.conf 文件

  2. 重启服务:docker-compose restart

常用配置选项:

  • 设置密码:取消注释并修改 requirepass yourstrongpassword

  • 调整内存限制:修改 maxmemory 值

  • 更改持久化策略:设置 appendonly no 禁用 AOF

  • 启用集群模式:取消注释集群相关配置

8. 数据持久化说明

  • 数据保存在 ./data 目录中:

    • dump.rdb:RDB 快照文件

    • appendonly.aof:AOF 日志文件

  • 即使容器重启或删除,数据也会保留

  • 定期备份 data 目录以确保数据安全

9. 升级 Redis 版本

要升级 Redis 版本:

  1. 修改 docker-compose.yml 中的镜像标签:

    yaml

    复制

    下载

    image: redis:7.2.5  # 新版本
  2. 执行升级:

    bash

    复制

    下载

    docker-compose down
    docker-compose pull
    docker-compose up -d

10. 安全建议

  1. 设置强密码

    • 在 redis.conf 中设置 requirepass

    • 连接时使用:redis-cli -a yourpassword

  2. 限制访问

    bash

    复制

    下载

    # 在服务器防火墙限制访问
    sudo ufw allow from 192.168.1.0/24 to any port 6379
  3. 启用 TLS(高级):

    conf

    复制

    下载

    # 在 redis.conf 中添加
    tls-port 6379
    tls-cert-file /path/to/redis.crt
    tls-key-file /path/to/redis.key

这个安装方案提供了 Redis 7.2.4 的稳定运行环境,包含了合理的默认配置和持久化设置,适合生产环境使用。

[root@centos08 work]# cd /software/wenxue/

[root@centos08 wenxue]# ls

docker-compose-linux-x86_64 milvsu.tar milvus-docker-compose.yml

[root@centos08 wenxue]# mkdir redis-docker && cd redis-docker

[root@centos08 redis-docker]# mkdir -p data conf

[root@centos08 redis-docker]# ls

conf data

[root@centos08 redis-docker]# pwd

/software/wenxue/redis-docker

[root@centos08 redis-docker]# vi docker-compose.yml

[root@centos08 redis-docker]# ls

conf data docker-compose.yml

[root@centos08 redis-docker]# cat <<EOF > conf/redis.conf

> # Redis 7.2.4 基础配置

>

> bind 0.0.0.0

> port 6379

> protected-mode yes

> daemonize no

> supervised no

> pidfile /var/run/redis_6379.pid

> loglevel notice

> logfile ""

> databases 16

> always-show-logo no

>

> # 持久化配置

> save 900 1

> save 300 10

> save 60 10000

> stop-writes-on-bgsave-error yes

> rdbcompression yes

> rdbchecksum yes

> dbfilename dump.rdb

> dir /data

> appendonly yes

> appendfilename "appendonly.aof"

> appendfsync everysec

> no-appendfsync-on-rewrite no

> auto-aof-rewrite-percentage 100

> auto-aof-rewrite-min-size 64mb

> aof-load-truncated yes

> aof-use-rdb-preamble yes

>

> # 内存管理

> maxmemory 1gb

> maxmemory-policy allkeys-lru

> maxmemory-samples 5

>

> # 安全设置

> # requirepass yourstrongpassword

>

> # 高级设置

> timeout 0

> tcp-keepalive 300

> repl-backlog-size 1mb

> repl-backlog-ttl 3600

> replica-serve-stale-data yes

> replica-read-only yes

> repl-diskless-sync no

> repl-diskless-sync-delay 5

> repl-disable-tcp-nodelay no

> replica-priority 100

> lazyfree-lazy-eviction no

> lazyfree-lazy-expire no

> lazyfree-lazy-server-del no

> replica-lazy-flush no

>

> # 性能优化

> hz 10

> dynamic-hz yes

> aof-rewrite-incremental-fsync yes

> rdb-save-incremental-fsync yes

> activerehashing yes

> client-output-buffer-limit normal 0 0 0

> client-output-buffer-limit replica 256mb 64mb 60

> client-output-buffer-limit pubsub 32mb 8mb 60

>

> # 集群模式 (单节点不需要)

> # cluster-enabled no

> # cluster-config-file nodes.conf

> # cluster-node-timeout 15000

> # cluster-replica-validity-factor 10

> # cluster-migration-barrier 1

> # cluster-require-full-coverage yes

> EOF

[root@centos08 redis-docker]# ls

conf data docker-compose.yml

[root@centos08 redis-docker]# ls ./conf/

redis.conf

[root@centos08 redis-docker]# docker-compose up -d

WARN[0000] /software/wenxue/redis-docker/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion

[+] Running 9/9

✔ redis Pulled 61.2s

✔ 09f376ebb190 Pull complete 20.2s

✔ f89f5b53601e Pull complete 20.5s

✔ 45d9097e8d82 Pull complete 21.0s

✔ b14e0d774d3d Pull complete 21.5s

✔ e8f49d7b64bc Pull complete 22.8s

✔ 17bfefd8913f Pull complete 58.6s

✔ 4f4fb700ef54 Pull complete 58.8s

✔ d05f3a965cb3 Pull complete 59.1s

[+] Running 2/2

✔ Network redis-docker_redis-net Created 0.2s

✔ Container redis-server Started 1.7s

[root@centos08 redis-docker]# docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

9b33aebb8399 redis:7.2.4 "docker-entrypoint.s…" 57 seconds ago Up 54 seconds 0.0.0.0:6379->6379/tcp redis-server

321d1c59a8fa zilliz/attu:v2.3.3 "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:8000->3000/tcp milvus-attu

f0a8261e9f83 quay.io/coreos/etcd:v3.5.5 "etcd -advertise-cli…" 2 hours ago Up 2 hours 2379-2380/tcp milvus-etcd

8cb9513cf532 minio/minio:RELEASE.2023-03-20T20-16-18Z "/usr/bin/docker-ent…" 2 hours ago Up 2 hours (healthy) 0.0.0.0:9000-9001->9000-9001/tcp, 0.0.0.0:9090->9090/tcp milvus-minio

[root@centos08 redis-docker]# docker ps | grep redis

9b33aebb8399 redis:7.2.4 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:6379->6379/tcp redis-server

[root@centos08 redis-docker]#


 


 

http://www.lryc.cn/news/2401851.html

相关文章:

  • 35.x64汇编写法(二)
  • 安全大模型的思考
  • SQL Server 2025 预览版新功能
  • NineData云原生智能数据管理平台新功能发布|2025年5月版
  • 数学复习笔记 25
  • Linux可执行文件ELF文件结构
  • RAG:大模型微调的革命性增强——检索增强生成技术深度解析
  • DisplayPort 2.0协议介绍(1)
  • I2C通信讲解
  • 【信息系统项目管理师-选择真题】2025上半年(第一批)综合知识答案和详解
  • ABP VNext 在 Kubernetes 中的零停机蓝绿发布
  • linux 故障处置通用流程-36计-14-27
  • https和http有什么区别-http各个版本有什么区别
  • 基于回归算法的心理健康预测(EDA + 预测)
  • React Native开发鸿蒙运动健康类应用的项目实践记录
  • 【新品解读】一板多能,AXRF49 定义新一代 RFSoC FPGA 开发平台
  • 贪心算法应用:线性规划贪心舍入问题详解
  • YOLO在C#中的完整训练、验证与部署方案
  • 洛谷题目:P2761 软件补丁问题 (本题简单)
  • 智慧园区数字孪生全链交付方案:降本增效30%,多案例实践驱动全周期交付
  • 【OpenGL学习】(四)统一着色和插值着色
  • 42、响应处理-【源码分析】-浏览器与PostMan内容协商完全适配
  • 在 CentOS 上安装 Docker 和 Docker Compose 并配置使用国内镜像源
  • Java Lambda表达式深度解析:从入门到实战
  • Docker慢慢学
  • cursor-free-vip使用
  • 使用SSH tunnel访问内网的MySQL
  • Redis持久化模式RDB与AOF
  • 【JS进阶】ES5 实现继承的几种方式
  • 【数据结构】树形结构--二叉树(二)