共计 1708 个字符,预计需要花费 5 分钟才能阅读完成。
导读 | 大家好,本篇文章主要讲的是关于 docker 容器部署 redis 步骤介绍,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览 |
1 redis 配置文件
官方下载:redis.conf
路径:在容器中,一般可以保存在 /etc/redis/redis.conf 路径中
配置文件详解,根据实际情况进行修改:
# 这里的 bind 指的是只有指定的网段才可以访问 redis,注释后则没有这个限制 | |
# bind 127.0.0.1 | |
# 默认端口为 6379 | |
port 6379 | |
# daemonize 表示是否以守护进程进行执行,容器中执行必须设置成 no | |
# 容器中如果设置成 yes,那么会和 docker run 中的 - d 冲突,导致启动失败 | |
daemonize no | |
# protected-mode | |
# 设置成 yes 表示开启保护模式,无法远程访问 | |
# 设置成 no 则表示表示保护模式,可以进行远程访问 | |
protected-mode no | |
# 注释掉则默认 redis 密码为空 | |
# 启用,则后面 redis123 为 reids 登录密码 | |
requirepass redis123 | |
# databases 设置数据库个数 | |
databases 16 | |
# save | |
save 900 1 | |
save 300 10 | |
save 60 10000 | |
# 默认不开启 aof 模式,默认是 rdb 方式持久化 | |
appendonly yes # 改为 yes 启用 aof 功能 | |
appendfilename "appendonly.aof" # 持久化文件的名字 | |
# appendfsync always # 每次修改都会 sync,消耗性能 | |
appendfsync everysec # 每秒执行一次 sync,可能会丢失这 1s 的数据 | |
# appendfsync no # 不执行 sync,操作系统会自动同步数据 |
2 docker 命令启动
启动命令
docker run -p 6379:6379 \ | |
-v /Users/chenbinhao/redis_6379/data:/data \ | |
-v /Users/chenbinhao/redis_6379/config/redis.conf:/etc/redis/redis.conf \ | |
-d redis redis-server /etc/redis/redis.conf --appendonly yes |
参数说明
-p 端口映射,redis 默认端口为 6379
-v 主要映射两个路径中的文件
/data redis 容器中会将数据保存在该路径中,此处映射是为了持久化保存数据。
/etc/redis/redis.conf 自定义配置文件保存的位置,此处映射是为了启动时可以指定自定义配置文件。
-d 表示在后台以守护进程进行运行。注意:redis.conf 配置文件中需要配置 daemonize no,否则无法将无法启动成功。
redis-server /etc/redis/redis.conf –appendonly yes 启动 redis 命令,如果以自定义配置文件启动,则需要执行此命令。
日志查看:docker logs containerID 如果启动失败使用此命令进行查看失败日志,根据日志进行调试
3 docker-compose 启动
目录结构
├─reids_6379 | |
│ ├─docker-compose.yml | |
│ ├─config | |
│ │ └─redis.conf | |
│ └─data | |
│ │ └─.. |
配置 docker-compose.yml 文件
version: '3' | |
services: | |
redis: | |
image: redis:latest | |
restart: always | |
ports: | |
- "6379:6379" | |
volumes: | |
- "./data:/data" | |
- "./config/redis.conf:/etc/redis/redis.conf" | |
command: redis-server /etc/redis/redis.conf |
启动命令
启动:在 docker-compose.yml 所在目录中执行 docker-compse up -d
停止并删除:docker-compose down
到此这篇关于关于 docker 容器部署 redis 步骤介绍的文章就介绍到这了 >
正文完
星哥玩云-微信公众号
