目前 5.x 系列中 5.0.9 为最新版本。
docker pull redis:5.0.9
安装最新版本的 redis.
docker pull redis:latest
直接运行 redis
docker run --name myredis -d redis
或者设置为后台运行
docker run --name myredisNew -itd -p 6379:6379 redis:latest
进入容器命令
docker exec -it myredisNew /bin/bash
或者自定义配置 conf
docker run -itd --name my-redis -p 6379:6379 \ -v /home/docker-config/redis-config/redis.conf:/etc/redis/redis.conf \ redis:5.0.9 \ redis-server /etc/redis/redis.conf \ --appendonly yes
参数说明:
appendonly yes:开启数据持久化
从 http://download.redis.io/releases/redis-5.0.9.tar.gz 官网扒下
redis.conf配置文件。用于解决创建 Redis 容器没有 conf 配置文件的问题我们使用 grep 命令来简单的处理一个下,然后就可以看到redis.conf 的所有配置信息,但是现在输入的命令是不可以编辑的
命令
cat redis.conf | grep -v "#" | grep -v "^$"
redis.conf 修改说明
- 将
bind 127.0.0.1 ::1绑定的主机地址这一行注释掉。
- 将
protected-mode要设置成 no (默认为yes的, 防止了远程访问,在 redis3.2.3 版本后)
- 设置密码
- 关闭守护进程
daemonize noredis 默认不是以守护进程的方式运行,在开发过程中我们都会把这个配置开启 配置为 yes 。
然后在回顾一下 docker run 命令里边有一个参数-d这个参数也是以守护进程执行。
这下应该就很清楚了,是 redis.conf 跟 docker 配置冲突了
打开配置文件把守护进程修改为no
# bind 127.0.0.1 protected-mode no requirepass useaverystrongpasswordfoobared666useaverystrongpassword888useaverystrongpassword999useaverystrongpassword110 daemonize no
redis.conf 提取的完整版
protected-mode no port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile "" databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir ./ 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 requirepass useaverystrongpasswordfoobared666useaverystrongpassword888useaverystrongpassword999useaverystrongpassword110 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no appendonly no 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 lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 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 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes
通过 redis-cli 查看连接
# 直接登录 docker exec -it myredis redis-cli # 加 -a 表示通过密码登录 docker exec -it myredis redis-cli -a useaverystrongpasswordfoobared666useaverystrongpassword888useaverystrongpassword999useaverystrongpassword110
使用 docker logs 观察 Redis 启动效果
docker logs myredis