[英].NET service can't connect to Redis
I'm trying to get a .NET API to be able to connect to a Redis instance on my docker network.我正在尝试获取一个 .NET API 以便能够连接到我的 docker 网络上的 Redis 实例。 Here's what I've got:
这是我所拥有的:
my-network
at localhost:6379
localhost:6379
自定义 docker 网络my-network
上启动并运行my-network
my-network
上启动并运行(Maybe?) Relevant versioning: (也许?)相关版本控制:
I've tried everything, network inspect
, adding and removing both containers to the network, even with the special connection string flag resolveDns=true
in my .NET code.我已经尝试了一切,
network inspect
,添加和删除两个容器到网络,即使在我的 .NET 代码中使用特殊的连接字符串标志resolveDns=true
。 No matter what I do I get:无论我做什么,我都会得到:
StackExchange.Redis.RedisConnectionException: It was not possible to connect to the redis server(s). UnableToConnect on localhost:6379/Interactive, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync, outstanding: 0, last-read: 0s ago, last-write: 0s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 0s ago, v: 2.1.58.34321
I'm losing my mind here, Redis is up and running at localhost:6379
on the docker network, why can't .NET connect?我在这里发疯了,Redis 已在 docker 网络上的
localhost:6379
上启动并运行,为什么无法连接 .NET?
YES I've passed a custom redis.conf which has bind 0.0.0.0
as well.是的,我已经通过了一个自定义的 redis.conf,它也
bind 0.0.0.0
。
Thanks to Hans Kilian's comment, I was able to finally connect!感谢 Hans Kilian 的评论,我终于可以连接了!
Despite what seemingly infinite Redis + Docker tutorials say on the using localhost:6379
, this is in fact not the case when Redis is running in a Docker network.尽管看似无穷无尽的 Redis + Docker 教程在 using
localhost:6379
上说了些什么,但实际上当 Redis 在 Docker 网络中运行时,情况并非如此。 To find that IP, for example with a container named redis
, issue:要查找该 IP,例如使用名为
redis
的容器,请发出:
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis
It is then the resulting IP address, with port 6379, that you would need to provide as your connection string.然后,您需要将生成的 IP 地址和端口 6379 作为连接字符串提供。
tl;dr; tl;博士;
not:不是:
localhost:6379
but the Docker assigned IP for the container, ex.:但是 Docker 为容器分配了 IP,例如:
172.18.0.7:6379
EDIT: You can also use the container name instead of the IP, ie:编辑:您也可以使用容器名称而不是 IP,即:
redis:6379
Hope this helps anyone who may stumble upon this.希望这可以帮助任何可能偶然发现此问题的人。
I fixed this by changing:我通过更改解决了这个问题:
CacheSettings:ConnectionString=basketdb:6379
to到
CacheSettings__ConnectionString=basketdb:6379
in the docker-compose.override.yml在docker-compose.override.yml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.