Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can StackExchange.Redis connection without port work in Docker bridge network? #2562

Closed
william19821101 opened this issue Sep 27, 2023 · 5 comments

Comments

@william19821101
Copy link

I am developing an ASP.NET Core project and using StackExchange.Redis to connect to a Redis instance running in a Docker container. My .NET project is also running in a Docker container, and both containers are in the same Docker bridge network.

The Redis container is named "redis", so this way works => ConnectionMultiplexer.Connect("redis:6379,abortConnect=false,password=xxx")

However, if I remove the port from the connection string, it doesn't work => ConnectionMultiplexer.Connect('redis,abortConnect=false,password=xxx'). I receive the message: "The specified endpoint is not defined (Parameter 'endpoint')"

I'm aware that we can use container name as a host to connect without specifying the port in the same Docker bridge network. But is it necessary to include the port in the StackExchange.Redis connection?

@mgravell
Copy link
Collaborator

I'm intrigued - those two things should be functionally identical... I'll see what I can find.

@mgravell
Copy link
Collaborator

mgravell commented Sep 28, 2023

With a local hosts entry for redis, this works fine:

using StackExchange.Redis;

using var muxer = ConnectionMultiplexer.Connect("redis,abortConnect=false,password=xxx");
var ttl = await muxer.GetDatabase().PingAsync();
Console.WriteLine($"connected; ttl={ttl.TotalMilliseconds}ms");

with output:

connected; ttl=0.6956ms

Do you have a runnable repro? I'm not seeing any failure here. Also: what library version are you using? Finally, is there a stack trace on that message, so we can see where it is originating?

@mgravell
Copy link
Collaborator

mgravell commented Sep 28, 2023

I think what you're actually doing is this:

var server = muxer.GetServer("redis");

Now, that parameter is called hostAndPort, and that: currently requires the port. I think the real question here is:

Can GetServer(string) apply the same default port logic that Connect uses when parsing endpoints referred only as a host?

That's not a terrible idea, but today: it doesn't do that.

Workaround for today:

var server = muxer.GetServer(muxer.GetEndPoints().Single());

or

var server = muxer.GetServers().Single();

@mgravell
Copy link
Collaborator

mgravell commented Sep 28, 2023

So: I did a spike, and the changes needed to do this aren't huge, but: it adds complexity. The only scenario I can think of when people don't want to be really precise with their endpoints is: single endpoint - and in that scenario, the two approaches shown above already give a simple solution. They're also more efficient, I think - as to do this the magic way would require two sets of endpoint allocation.

IMO: leave it alone.

Before I conclude that, have I correctly interpreted the scenario, and do the workarounds shown above work?

@william19821101
Copy link
Author

Hi Marc,
Thank you for correcting my question accurately. Yes, I did what you thought I hadn't mentioned in my post, and that seems to be the point.
var server = muxer.GetServer("redis");

And I've tried your solution that perfectly meets my needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants