环境:
OS:Centos 7
redis:7.4.0
1.准备redis配置文件
[root@localhost redisconf]# more redis.conf ##daemonize yes dir ./ port 6379 tcp-backlog 511 timeout 300 tcp-keepalive 300 loglevel notice logfile "redis-6379.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump-6379.rdb slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 appendonly yes appendfilename "redis-6379.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated 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 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes # Generated by CONFIG REWRITE masterauth "hxl123" requirepass "hxl123" protected-mode yes maxmemory-policy allkeys-lfu maxmemory 4gb maxmemory-samples 5
关键点:
daemonize必须设置为no或是注释掉
2.准备目录
mkdir -p /home/middle/redis_docker_compose ##docker-compose.yml
mkdir -p /home/middle/redis_docker_compose/redis/config ##这个目录需要提前创建好,redis配置文件存放到该目录
3.准备yaml配置文件
[root@localhost redis_docker_compose]# more docker-compose.yml version: '3' services:redis:image: registry.cn-shenzhen.aliyuncs.com/hxlk8s/redis:7.4container_name: redisrestart: alwaysports:- "6379:6379"volumes:- "./redis/data:/data"- "./redis/config:/etc/redis"command: redis-server /etc/redis/redis.conf
关键点说明:
a.需要外挂整个配置目录,而不是单个配置文件
正确的:
- "./redis/config:/etc/redis"
错误的:
- "./redis/config/redis.conf:/etc/redis/redis.conf"
这种情况会出现使用config rewrite 保持修改提示权限不足的问题
192.168.1.134:6379> config rewrite
(error) ERR Rewriting config file: Permission denied
b.数据外挂目录./redis/data,不需要提前创建,容器启动的时候会自动创建.
4.启动
[root@localhost redis_docker_compose]# cd /home/middle/redis_docker_compose
[root@localhost redis_docker_compose]# docker compose up -d
5.客户端尝试登录
[root@host135 bin]# /usr/local/services/redis/bin/redis-cli -h 192.168.1.134 -a hxl123 -p 6379
尝试修改参数,然后保存
192.168.1.134:6379> config set maxmemory 5GB
OK
192.168.1.134:6379> config rewrite
(error) ERR Rewriting config file: Permission denied
提示权限不足
6.登录容器修改配置目录权限
docker exec -ti redis /bin/bash
root@4129d74bc933:/data# ls -al
total 4
drwxr-xr-x 3 redis root 49 Jan 10 07:32 .
drwxr-xr-x 1 root root 17 Jan 10 07:32 ..
drwx------ 2 redis redis 103 Jan 10 07:32 appendonlydir
-rw------- 1 redis redis 1284 Jan 10 07:32 redis-6379.log
我们看到容器里面的redis是使用redis账号启动的,我们查看下配置文件所在的目录权限
root@49cb89886715:/data# cd /etc/redis
root@49cb89886715:/etc/redis# ls -al
total 4
drwxrwxr-x 2 root root 24 Jan 10 07:39 .
drwxr-xr-x 1 root root 19 Jan 10 07:40 ..
-rw-rw-r-- 1 root root 1246 Jan 10 07:39 redis.conf
修改容器里的目录权限为redis
root@4129d74bc933:/etc/redis# chown -R redis:redis /etc/redis
再次保持执行就可以了
192.168.1.134:6379> config rewrite
OK