Redis Docker - 无法从 C# 客户端连接

Posted

技术标签:

【中文标题】Redis Docker - 无法从 C# 客户端连接【英文标题】:Redis Docker - Unable to connect from C# client 【发布时间】:2015-07-09 08:14:15 【问题描述】:

我是 docker 和 redis 的新手,我使用以下命令在 docker 上运行 redis 3.0:

docker run --name redisDev -d redis

连接了端口 6379 后似乎可以正常启动:

docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
b95c9402dd42        redis:3             "/entrypoint.sh redi   47 minutes ago      Up 47 minutes       6379/tcp            redisDev

我正在尝试使用以下代码进行连接:

        var sb = new StringBuilder();
        var tw = new StringWriter(sb);
        ConnectionMultiplexer redis;
        try
        
            redis = ConnectionMultiplexer.Connect("vb-haproxy01.verify.local", tw);
        
        catch (Exception ex)
        
            //Console.WriteLine(ex.Message);
            tw.Flush();

            Console.WriteLine(sb.ToString());
            return;
        

我收到以下错误:

vb-haproxy01.verify.local:6379

1 unique nodes specified
Requesting tie-break from vb-haproxy01.verify.local:6379 > __Booksleeve_TieBreak
...
Allowing endpoints 00:00:05 to respond...
vb-haproxy01.verify.local:6379 faulted: SocketFailure on PING
vb-haproxy01.verify.local:6379 failed to nominate (Faulted)
> UnableToResolvePhysicalConnection on GET
No masters detected
vb-haproxy01.verify.local:6379: Standalone v2.0.0, master; keep-alive: 00:01:00;
 int: Connecting; sub: Disconnected; not in use: DidNotRespond
vb-haproxy01.verify.local:6379: int ops=0, qu=0, qs=0, qc=1, wr=0, sync=1, socks
=2; sub ops=0, qu=0, qs=0, qc=0, wr=0, socks=2
Circular op-count snapshot; int: 0 (0.00 ops/s; spans 10s); sub: 0 (0.00 ops/s;
spans 10s)
Sync timeouts: 0; fire and forget: 0; last heartbeat: -1s ago
resetting failing connections to retry...
retrying; attempts left: 2...
1 unique nodes specified

Linux 防火墙设置:

sudo iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:6379

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

我错过了什么?

【问题讨论】:

【参考方案1】:

问题是主机上的端口 6379 没有将端口 6379 转发到 docker。以下命令的“-p 6379:6379”修复了问题:

docker run -d --name redisDev -p 6379:6379 redis

【讨论】:

以上是关于Redis Docker - 无法从 C# 客户端连接的主要内容,如果未能解决你的问题,请参考以下文章

无法从外部 docker 连接到 dockerized redis 实例

【6.2.1】服务器安装 Docker Swarm中搭建 Redis集群

Docker搭建redis主从+哨兵

C# 和 Docker - 无法从容器化 .NET Core 3.1 Web API 连接到容器化 MySQL 服务器

Docker Compose——搭建Redis集群

如何从 C# StackExchange.Redis 获取多个 Redis 键的 TTL