Cannot connect to Redis hosted on local docker from a .Net Core MVC Application on my local machine RRS feed

  • Question

  • I have Redis hosted on my local docker (Port: 6379). I have created a .Net Core app on my local visual studio (note: the mvc app is not in docker) and trying to connect to the redis instance hosted on docker but getting the below exception:

      Message=One or more errors occurred. (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: 1s ago, last-write: 1s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 8s ago, v:

    Tried using both distributed cache using Microsoft.Extensions.Caching.StackExchangeRedis as well as StackExchange.Redis. Both failing to connect from my app.

    But if I login to docker CLI I can connect to Redis and add a key and retrieve value from cache.

    Any help is much appreciated..

    Wednesday, August 12, 2020 5:26 AM


All replies

  • Hi Debayan Mitra,

    In order to help you find the correct forum to go ask questions, I have moved the thread to 'Where is the forum for' forum.

    Thank you for your understanding.

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 12, 2020 8:11 AM
  • Create a new directory for your application.

    This directory is the context of your docker-compose project. For Docker Desktop for Windows and Docker Desktop for Mac, you need to set up file sharing for the volume that you need to map.

    Within your directory, use the dotnet:2.1-sdk Docker image to generate a sample web application within the container under the /app directory and into your host machine in the working directory:

    $ docker run -v ${PWD}:/app --workdir /app microsoft/dotnet:2.1-sdk dotnet new mvc --auth Individual
    Note: If running in Docker Desktop for Windows, make sure to use Powershell or specify the absolute path of your app directory.

    Create a Dockerfile within your app directory and add the following content:

    FROM microsoft/dotnet:2.1-sdk
    COPY . /app
    WORKDIR /app
    RUN ["dotnet", "restore"]
    RUN ["dotnet", "build"]
    EXPOSE 80/tcp
    RUN chmod +x ./entrypoint.sh
    CMD /bin/bash ./entrypoint.sh
    This file defines how to build the web app image. It uses the .NET Core SDK image, maps the volume with the generated code, restores the dependencies, builds the project and exposes port 80. After that, it calls an entrypoint script that we create in the next step.

    The Dockerfile makes use of an entrypoint to your webapp Docker image. Create this script in a file called entrypoint.sh and paste the contents below.

    Note: Make sure to use UNIX line delimiters. The script doesn’t work if you use Windows-based delimiters (Carriage return and line feed).


    set -e
    run_cmd="dotnet run --server.urls http://*:80"

    until dotnet ef database update; do
    >&2 echo "SQL Server is starting up"
    sleep 1

    >&2 echo "SQL Server is up - executing command"
    exec $run_cmd
    This script restores the database after it starts up, and then runs the application. This allows some time for the SQL Server database image to start up.

    Create a docker-compose.yml file. Write the following in the file, and make sure to replace the password in the SA_PASSWORD environment variable under db below. This file defines the way the images interact as independent services.

    Note: The SQL Server container requires a secure password to startup: Minimum length 8 characters, including uppercase and lowercase letters, base 10 digits and/or non-alphanumeric symbols.

    version: "3"
            build: .
                - "8000:80"
                - db
            image: "mcr.microsoft.com/mssql/server"
                SA_PASSWORD: "Your_password123"
                ACCEPT_EULA: "Y"
    This file defines the web and db micro-services, their relationship, the ports they are using, and their specific environment variables.

    Note: You may receive an error if you choose the wrong Compose file version. Be sure to choose a version that is compatible with your system.

    Go to Startup.cs and locate the function called ConfigureServices (Hint: it should be under line 42). Replace the entire function to use the following code (watch out for the brackets!).

    Note: Make sure to update the Password field in the connection variable below to the one you defined in the docker-compose.yml file.

    public void ConfigureServices(IServiceCollection services)
        // Database connection string.
        // Make sure to update the Password value below from "Your_password123" to your actual password.
        var connection = @"Server=db;Database=master;User=sa;Password=Your_password123;";

        // This line uses 'UseSqlServer' in the 'options' parameter
        // with the connection string defined above.
            options => options.UseSqlServer(connection));

        services.AddIdentity<ApplicationUser, IdentityRole>()


        // Add application services.
        services.AddTransient<IEmailSender, AuthMessageSender>();
        services.AddTransient<ISmsSender, AuthMessageSender>();
    Go to app.csproj. You see a line like:

    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="1.1.2" />
    The generated project uses sqlite by default. To use SQL Server, add this line to app.csproj:

    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
    The Sqlite dependency was at version 1.1.2 at the time of this writing. Use the same version for the SQL Server dependency.

    Ready! You can now run the docker-compose build command.

    $ docker-compose build
    Make sure you allocate at least 2GB of memory to Docker Engine. Here is how to do it on Docker Desktop for Mac and Docker Desktop for Windows. This is necessary to run the SQL Server on Linux container.

    Run the docker-compose up command. After a few seconds, you should be able to open localhost:8000 and see the ASP.NET core sample website. The application is listening on port 80 by default, but we mapped it to port 8000 in the docker-compose.yml.

    $ docker-compose up
    Go ahead and try out the website! This sample uses the SQL Server database image in the back-end for authentication.
    Wednesday, August 12, 2020 10:52 AM
  • Might try asking for help over here.





    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    • Proposed as answer by Guido Franzke Thursday, August 13, 2020 5:54 AM
    • Marked as answer by Dave PatrickMVP Saturday, August 22, 2020 10:14 PM
    Wednesday, August 12, 2020 1:08 PM