简单介绍docker容器部署redis步骤

导读 大家好,本篇文章主要讲的是关于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步骤介绍的文章就介绍到这了>

原文来自:https://www.jb51.net/article/230773.htm

本文地址:https://www.linuxprobe.com/docker-redis-linux.html编辑:向云艳,审核员:逄增宝

Linux命令大全:https://www.linuxcool.com/

© 版权声明
THE END
喜欢就支持一下吧