1.拉取镜像:
docker push redis
2.准备redis配置文件
首先在/root/redis/data 创建好文件夹用于存放redis数据
然后从redis官网下载redis.conf文件(http://download.redis.io/redis-stable/redis.conf)到/root/redis/
wget http://download.redis.io/redis-stable/redis.conf
以下是一些可能修改的配置:
daemonize no#用守护线程的方式启动
requirepass yourpassword#给redis设置密码
bind 127.0.0.1 #注释掉这部分,这是限制redis只能本地访问
protected-mode yes#取消保护模式允许其他主机连接redis,改为no
appendonly yes#redis持久化
Redis可以实现数据的持久化存储,即将数据保存到磁盘上。
Redis的持久化存储提供两种方式:RDB与AOF。RDB是默认配置。AOF需要手动开启。
现在Redis的配置中默认是关闭AOF模式的。
如果要开启AOF模式,修改Redis的配置文件redis.conf。
相关的配置项:
appendonly yes #开启AOF模式 原文1
appendfilename "appendonly.aof" #保存数据的AOF文件名称 原文1
# appendfsync always
appendfsync everysec #fsync模式 原文2
# appendfsync no
no-appendfsync-on-rewrite no #原文3
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb #原文4
aof-load-truncated yes #原文5
3.启动Redis:
docker run -p 6379:6379 --name redis -v /root/redis/redis.conf:/etc/redis/redis.conf -v /root/redis/data:/data -d redis redis-server /etc/redis/redis.conf
4. 查看启动结果:
# docker logs redis
1:C 23 Dec 2018 13:30:01.426 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 23 Dec 2018 13:30:01.426 # Redis version=5.0.2, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 23 Dec 2018 13:30:01.426 # Configuration loaded
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.2 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
1:M 23 Dec 2018 13:30:01.427 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 23 Dec 2018 13:30:01.427 # Server initialized
1:M 23 Dec 2018 13:30:01.427 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 23 Dec 2018 13:30:01.427 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 23 Dec 2018 13:30:01.427 * Ready to accept connections