Windows 10 Pro下Docker中Redis无法连接问题求助
Docker Redis容器连接超时问题求助
容器信息
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES edc955eceb27 redis:5 "docker-entrypoint.s…" 16 hours ago Up 2 hours 0.0.0.0:6379->6379/tcp sad_lumiere
容器IP地址
$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' edc 172.17.0.2
Redis容器日志
1:C 09 Nov 2023 07:35:16.706 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:C 09 Nov 2023 07:35:16.707 # Redis version=5.0.14, bits=64, commit=00000000, modified=0, pid=1, just started 1:C 09 Nov 2023 07:35:16.707 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 1:M 09 Nov 2023 07:35:16.712 * Running mode=standalone, port=6379. 1:M 09 Nov 2023 07:35:16.712 # 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 09 Nov 2023 07:35:16.712 # Server initialized 1:M 09 Nov 2023 07:35:16.713 # 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 09 Nov 2023 07:35:16.714 * DB loaded from disk: 0.001 seconds 1:M 09 Nov 2023 07:35:16.714 * Ready to accept connections
已执行的排查步骤
- 将连接IP从
172.17.0.2改为127.0.0.1尝试连接 - 关闭Windows Defender防火墙
- 执行
netstat -ano | findstr "6379"验证端口6379已开放 - 用Python脚本测试连接,触发
TimeoutError:
脚本代码:
错误输出:import redis redis_client = redis.Redis(host='172.17.0.2', port=6379, decode_responses=True) redis_client.set('my_key', 'Hello, Redis!') value = redis_client.get('my_key') print(value)TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond - 用Telnet测试连接失败:
C:\WINDOWS\system32>telnet 172.17.0.2 6379 Connecting To 172.17.0.2... Could not open connection to the host, on port 6379: Connect failed
分析与建议
- 优先使用宿主机端口映射访问:容器已配置
0.0.0.0:6379->6379/tcp端口映射,直接用127.0.0.1:6379或宿主机IP访问即可,无需使用容器内部IP172.17.0.2——Windows环境下Docker的容器内部IP属于虚拟网络,宿主机直接访问可能存在网络隔离问题。 - 检查Redis绑定配置:默认配置下Redis可能仅绑定
127.0.0.1,导致容器外部无法访问。可重启容器时指定绑定所有地址:docker run -d -p 6379:6379 redis:5 redis-server --bind 0.0.0.0 - 验证容器内服务可用性:先进入容器内部测试Redis是否正常运行,排除服务本身问题:
若能正常执行docker exec -it sad_lumiere redis-cliSET/GET命令,说明Redis服务无问题,故障点在网络连通性。 - 检查Docker虚拟网络配置:重启Docker Desktop服务,确认WSL 2后端(若使用)的虚拟网卡状态正常,可通过
ipconfig查看vEthernet (DockerNAT)的IPv4地址是否存在。 - 添加宿主机路由条目:若必须使用容器IP访问,检查宿主机是否有到
172.17.0.0/16网段的路由,缺失则手动添加(需管理员权限):route add 172.17.0.0 mask 255.255.0.0 <Docker虚拟网卡IP>
内容的提问来源于stack exchange,提问作者sierra_papa
相关产品推荐
相关产品推荐

