skip to Main Content

I’m using Parallels on my Mac to run a Windows 11 VM. Docker Desktop is running on the Mac. I’ve created an Aspire.NET 9 app that uses Redis. The app is developed using VS Code on the Mac and Visual Studio 2022 on Windows. The Mac version works fine, but the Windows version is struggling to connect to Redis.

The host C# application code is:

var builder = DistributedApplication.CreateBuilder(args);

var cache = builder.AddRedis("cache", 6379);

var apiService = builder.AddProject<Projects.AspireApp1_ApiService>("apiservice");

builder.AddProject<Projects.AspireApp1_Web>("webfrontend")
    .WithExternalHttpEndpoints()
    .WithReference(cache)
    .WithReference(apiService);

builder.Build().Run();

How do I change the Redis endpoint from localhost to the Mac’s IP address? Currently, the endpoint is localhost, and I’m having difficulty finding a solution.

I am using shared network and changed based on a suggestion by Maick the appsettings file to use "Services" instead "ConnectionStrings"

"Services": {
  "RedisAspire": {
    "tcp": [ "redismac:50693" ]
  }
}

still getting error

fail: Microsoft.Extensions.Hosting.Internal.Host[11] Hosting failed to start
Aspire.Hosting.DistributedApplicationException: Container runtime ‘docker’ was found but appears to be unhealthy. The error from
the container runtime check was error during connect: Get
"http://redismac:2376/v1.47/containers/json?limit=1": dial tcp
10.0.0.204:2376: connectex: No connection could be made because the target machine actively refused it..

Seems that the "Services" settings is ignored.

the next thing i tried is to expose docker api via tcp/ip using socat command from the Mac.



socat TCP-LISTEN:2376,reuseaddr,fork UNIX-CONNECT:/var/run/docker.sock



the app now comes up and Aspire showing all apps (container, api, web) with “green” state but the endpoint for “RedisAspire” showing up as “tcp://localhost:53059” and the error that is comming from web app is:

UnableToConnect (None, 0-read, last-recv: 0) on
localhost:54272/Interactive, Flushed/ReadAsync, last: ECHO, origin:
ResetNonConnected, outstanding: 12, last-read: 5s ago, last-write

thank you

2

Answers


  1. Chosen as BEST ANSWER

    I’ve finally figured out the issue, and everything is working now! Here’s what I learned:

    The AddRedis() method is designed for local Redis instances only. To connect to an externally hosted Redis (like my Docker container on macOS), you need to use AddConnectionString() instead.

    The AddConnectionString() method requires a properly defined "ConnectionStrings" section in the appsettings.json file. For example:

    "ConnectionStrings": {
        "cache": "hostname:port"
    }
    

    Many articles provide incorrect or outdated information, especially for .NET 8 and .NET 9 Aspire. It’s best to refer directly to the source code for accurate guidance.

    Starting Redis container in Docker on my mac in detached mode and mapping the Redis port to the port the app uses:

    docker run -d -p 50140:6379 --name redis redis:latest
    

  2. Not sure about your network setup between macos and the windows VM with parallels (bridge or shared), I think the best option is shared, so windows will behave as another pc in the network and macos will be like a remote host in the same network, with this in mind your visual studio and dotnet does not know where docker desktop is located, so you need to configure how they connect to docker desktop as a remote service, same for your projects (you can not set an IP when configuring a host app like redis).

    — With bridge
    you need to make port mappings and firewall configurations between macos and vm for all ports and protocols you plan to use, including docker, docker daemons… redis, but even with that you will find that net aspire will try to create a bridge between host 6379 port and a random port that will be mapped to redis container 6379 port and I have not be able to set that random port to a static value just be able to create another route using withendpoint method but need aspire still uses the random port for defaultservices and other tools like rediscommander if you want to have it…

    —"shared mode"
    Use shared mode in parallels to avoid cumbersome network configuration… in netaspire host program.cs check for the host running the build, if it is Mac use:

    If(isRunningOnMac) { 
    // create the docker container in Mac with persist lifecycle, open a static port and map it to 6379
    buider.AddRedis(6379)
    .AddEndpoint(name:"NotRandomEndpointPort26379to6379", port:26379, targetPort: 6379, isProxied:false, isExternal:true)
    .WithLifetime(ContainerLifetime.Persistent);
    }
    

    Map service discovery in your projects to find the macipfromwindows:26379
    To configure service discovery in appsettings add:

    "Services":{
    "RedisAspire":{
    "tcp":["macipfromwindows:26379"]
    }
    }
    

    In your projects program.cs when injecting redis use the same name of RedisAspire.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search