.NET service can't connect to Redis

Viewed 848

I'm trying to get a .NET API to be able to connect to a Redis instance on my docker network. Here's what I've got:

  • Redis container: up and running on custom docker network my-network at localhost:6379
  • .NET service: trying to get up and running on custom docker network my-network

(Maybe?) Relevant versioning:

  • Abp.RedisCache 5.4.0
  • StackExchange.Redis 2.1.58

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. 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?

YES I've passed a custom redis.conf which has bind 0.0.0.0 as well.

1 Answers

Thanks to Hans Kilian's comment, I was able to finally connect!

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. To find that IP, for example with a container named redis, issue:

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.

tl;dr;

not:

localhost:6379

but the Docker assigned IP for the container, ex.:

172.18.0.7:6379

EDIT: You can also use the container name instead of the IP, i.e.:

redis:6379

Hope this helps anyone who may stumble upon this.

Related