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:
my-network at localhost:6379my-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. 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.
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.
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'
"ConnectionStrings": {
"redis": "redis_cache:6379"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With