一、环境
系统:centos8
redis镜像: 5.0.12
springboot: 2.4.1
二、问题描述
2.1 容器启动命令:
docker run -itd --privileged=true -p 6379:6379 -v /root/redisDir/conf/redis.conf:/etc/redis/redis.conf -v /root/redisDir/data:/data --name redis dockerImages/redis:5.0.12 redis-server /etc/redis/redis.conf --appendonly yes
2.2 在宿主机启动springboot服务,连接docker容器启动的redis服务,出现无法连接的问题,具体报错日志
org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis;
nested exception is org.springframework.data.redis.connection.PoolException: Could not get a resource from the pool;
nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 127.0.0.1:6379
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.translateException(LettuceConnectionFactory.java:1553)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1461)
Caused by: org.springframework.data.redis.connection.PoolException: Could not get a resource from the pool;
nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 127.0.0.1:6379
at org.springframework.data.redis.connection.lettuce.LettucePoolingConnectionProvider.getConnection(LettucePoolingConnectionProvider.java:109)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1459)
... 78 more
Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to 127.0.0.1:6379
at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:78)
Caused by: io.lettuce.core.RedisConnectionException: Connection closed prematurely
at io.lettuce.core.protocol.RedisHandshakeHandler.channelInactive(RedisHandshakeHandler.java:86)
三、问题分析
1、在默认的redis.conf中bind只监听127.0.0.1,且保护状态为保护状态
四、问题解决方法
1、注释掉bind属性:#bind 127.0.0.1
2、protected-mode yes改为protected-mode no
3、设置requirepass,默认为foobared,修改为自定义密码或者通过redis-cli config set命令修改
修改以上部分,重启docker,问题解决