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

使用 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.xdnf.cn/news/11018.html

相关文章:

  • Python基于PCA、PCA-kernel、LDA的同心圆数据降维项目实战
  • 2005-2022全国及各省家庭承包耕地流转总面积及经营耕地面积数据(无缺失)
  • 移动网页调试的多元路径:WebDebugX 与其他调试工具的组合使用策略
  • HarmonyOS Next 弹窗系列教程(2)
  • matlab实现掺杂光纤放大器的模拟
  • uniapp开发使用vue3组合式api,实现从vue模块中自动导入
  • Flotherm软件许可与硬件要求
  • 我的技术笔记
  • 《汇编语言》第14章 端口
  • Python Day41学习(日志Day8复习)
  • 基于蝙蝠算法的路径优化
  • Python语法基础篇(包含类型转换、拷贝、可变对象/不可变对象,函数,拆包,异常,模块,闭包,装饰器)
  • 对比ODR直接赋值的非原子操作和BSRR原子操作
  • 机器学习——主成分分析PCA
  • 07.MySQL内置函数
  • 开发体育比分平台,有哪些坑需要注意的
  • Gephi中的Isometric Layout 插件使用应该用什么数据格式
  • UE5 2D角色PaperZD插件动画状态机学习笔记
  • el-select 实现分页加载,切换也数滚回到顶部,自定义高度
  • 2025.5.28 需求文档的撰写
  • xxhash和md5
  • 接口自动化测试之pytest 运行方式及前置后置封装
  • 蓝桥杯_DS18B20温度传感器---新手入门级别超级详细解析
  • 多模态大模型中的Projector模块深度解析
  • 苍穹外卖--HttpClient
  • Nginx上传大文件的配置
  • 普中STM32F103ZET6开发攻略(四)
  • 【Prompt实战】国际翻译小组
  • I2C 通信协议
  • Java并发编程:读写锁与普通互斥锁的深度对比