拉取最新redis镜像
docker pull redis
创建宿主机上的映射文件目录,并授权
[root@1-0001 ~]# mkdir -p /opt/redis/data
[root@1-0001 ~]# touch /opt/redis/redis.conf
[root@1-0001 ~]# chmod -R +777 /opt/redis
运行redis,生成相应容器
-d 后台启动
-v 宿主机目录挂在到容器的地址,映射(挂载数据、日志、配置)
--appendonly yes 在Redis容器启动redis-server服务器并打开Redis持久化配置
[root@1-0001 ~]# docker run --restart=always --log-opt max-size=100m --log-opt max-file=2 -p 8300:8300 --name redis -v /opt/redis/redis.conf:/etc/redis/redis.conf -v /opt/redis/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yes --requirepass 123456
修改配置,/opt/redis/redis.conf/redis.conf
appendonly yes 启动Redis持久化功能
protected-mode no 关闭protected-mode模式,此时外部网络可以直接访问 (docker默认开启)
bind 0.0.0.0 设置所有IP都可以访问 (docker默认开启)
[root@1-0002 redis]# vim /opt/redis/redis.conf/redis.conf
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 8300
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
# redis密码
requirepass 123456
测试redis服务
[root@1-0001 ~]# docker exec -it redis /bin/bash
root@a40d46499c3a:/# redis-cli -p 8300 -a 123456
127.0.0.1:8300>
[root@1-0001 ~]# docker exec -it redis redis-cli -p 8300 -a 123456
127.0.0.1:8300>
# 查看当前redis有没有设置密码
127.0.0.1:8300> config get requirepass