Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 723
fullStackChris Avatar asked Nov 16 '25 13:11

fullStackChris


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

like image 101
fullStackChris Avatar answered Nov 18 '25 19:11

fullStackChris


  1. Create docker-compose.yml file with links
    version: '3.4'
    
    services:
      caching:
        image: ${DOCKER_REGISTRY-}caching
        build:
          context: .
          dockerfile: Caching/Dockerfile
        depends_on:
        - redis_cache
        links:
        - redis_cache
       
      redis_cache:
        image: "redis:latest"    
        container_name: cache
        ports:
            - '6379:6379'  

  1. Instead of localhost use the redis_cache name
"ConnectionStrings": {
  "redis": "redis_cache:6379"
}
like image 45
Marcin Sulecki Avatar answered Nov 18 '25 21:11

Marcin Sulecki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!