为什么 redis 指定配置文件启动,就直接退出了?

FROM ubuntu:jammy
RUN . /etc/os-release && cat > /etc/apt/sources.list <<EOF
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME} main restricted universe multiverse
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-updates main restricted universe multiverse
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-backports main restricted universe multiverse
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-security main restricted universe multiverse
EOF

RUN apt update
RUN apt install -y vim gcc g++ cmake make
RUN apt install -y redis-server
CMD ["redis-server", "/etc/redis/redis.conf"]

一运行就退出了

╭─pon@admini ~/opt/docker-compose-public/redis-cluster-custom  ‹master*› 
╰─➤  docker run --name rr --rm -it ponponon/redis-cluster 
╭─pon@admini ~/opt/docker-compose-public/redis-cluster-custom  ‹master*› 
╰─➤  

但是把 CMD 修改成 CMD ["redis-server"] 就是一切正常

╰─➤  docker run --name rr --rm -it ponponon/redis-cluster 
1:C 14 Apr 2023 03:04:42.776 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 14 Apr 2023 03:04:42.776 # Redis version=6.0.16, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 14 Apr 2023 03:04:42.776 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.0.16 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 1
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

1:M 14 Apr 2023 03:04:42.781 # Server initialized
1:M 14 Apr 2023 03:04:42.781 # 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 14 Apr 2023 03:04:42.783 * Ready to accept connections

为什么?


已解决,redis-server /etc/redis/redis.conf 启动的时候,/etc/redis/redis.conf 里面的 daemonize 是 yes,所以就变成了后台任务

修改如下

redis.conf

port 6379
bind 0.0.0.0

protected-mode no
daemonize no
另外几个都没有用,只有 daemonize no 有用

文件卷挂载

docker run --rm -it -v ./redis.conf:/etc/redis/redis.conf ponponon/redis-cluster

这下 ok 了

图片.png

╰─➤  docker run  --rm -it -v ./redis.conf:/etc/redis/redis.conf  ponponon/redis-cluster 
1:C 14 Apr 2023 03:12:49.561 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 14 Apr 2023 03:12:49.561 # Redis version=6.0.16, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 14 Apr 2023 03:12:49.561 # Configuration loaded
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.0.16 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 1
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

1:M 14 Apr 2023 03:12:49.566 # Server initialized
1:M 14 Apr 2023 03:12:49.566 # 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 14 Apr 2023 03:12:49.568 * Ready to accept connections


我以为 redis-serverredis-server /etc/redis/redis.conf 是等价的,但是现在看起来,并不是 redis-server 不会去加载 /etc/redis/redis.conf

阅读 3.2k
1 个回答

问题可能与 Docker 容器内的 Redis 服务进程有关。当 Redis 服务进程以前台模式启动时,容器会保持运行状态,因为它需要一个前台运行的进程。如果 Redis 服务进程在后台启动,Docker 容器会认为它没有任何运行中的进程而退出。

要解决这个问题,你需要确保 Redis 服务进程以前台模式启动。您可以通过在 /etc/redis/redis.conf 配置文件中设置 daemonize no 参数来实现这一点。

首先,你需要确保 redis.conf 文件中的 daemonize 配置项设置为 no。你可以使用以下命令来检查和修改配置文件:

# 查找配置文件中的 "daemonize" 行
grep "daemonize" /etc/redis/redis.conf

# 如果输出 "daemonize yes",则使用 sed 修改配置文件
sed -i 's/daemonize yes/daemonize no/' /etc/redis/redis.conf

# 再次查找配置文件中的 "daemonize" 行以验证更改
grep "daemonize" /etc/redis/redis.conf

如果你已经在 Docker 容器内部执行了这些命令,你需要重新构建并运行容器以应用更改。如果你正在使用 Dockerfile,可以将以下 RUN 指令添加到 Dockerfile 中:

RUN sed -i 's/daemonize yes/daemonize no/' /etc/redis/redis.conf

然后重新构建并运行你的 Docker 容器。这将确保 Redis 以前台模式启动,并防止容器意外退出。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题