title: How can I dockerize ASP.NET Core app and Redis together?
body: <p>I'm working with a Docker and try to connect redis and web application. This is my docker-compose file:</p>
  
  <pre><code>version: '3.7'
  
  services:  
    web:
      image: redistest:latest
      depends_on:
        - "redis_image"
      build:
        context: .
        dockerfile: Dockerfile
      ports:
        - "9901:80"
  
    redis_image:
      image: redis
      container_name: cache
      ports:
        - "6379:6379"
  </code></pre>
  
  <p>This is my ConnectionStrings:</p>
  
  <pre><code>"ConnectionStrings": {
      "redis": "localhost:6379,abortConnect=False"
    },
  </code></pre>
  
  <p>Dockerfile looks like this:</p>
  
  <pre><code>FROM microsoft/dotnet:sdk AS build-env
  WORKDIR /app
  EXPOSE 80
  
  COPY *.csproj ./
  RUN dotnet restore RedisTest.csproj
  
  COPY . ./
  RUN dotnet publish RedisTest.csproj -c Release -o out
  
  FROM microsoft/dotnet:aspnetcore-runtime
  WORKDIR /app
  COPY --from=build-env /app/out .
  ENTRYPOINT ["dotnet", "RedisTest.dll"]
  </code></pre>
  
  <p>Connection to Redis in Startup.cs:</p>
  
  <pre><code>services.AddDistributedRedisCache(option =&gt;
  {
      option.Configuration = Configuration.GetConnectionString("redis");
  }); 
  </code></pre>
  
  <p>To connect to docker I write in cmd next commands:</p>
  
  <pre><code>docker-compose build
  docker-compose up
  </code></pre>
  
  <p>This commands work good, but after I go to "localhost:9901", I receive next errors in console</p>
  
  <pre><code>web_1          | fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
  web_1          |       An unhandled exception has occurred while executing the request.
  web_1          | StackExchange.Redis.RedisConnectionException: No connection is available to service this operation: EVAL; SocketFailure on localhost:6379/Subscription, origin: Error, input-buffer: 0, outstanding: 0, last-read: 0s ago, last-write: 0s ago, unanswered-write: 1291s ago, keep-alive: 60s, pending: 0, state: Connecting, last-heartbeat: never, last-mbeat: -1s ago, global: 0s ago ---&gt; StackExchange.Redis.RedisConnectionException: SocketFailure on localhost:6379/Subscription, origin: Error, input-buffer: 0, outstanding: 0, last-read: 0s ago, last-write: 0s ago, unanswered-write: 1291s ago, keep-alive: 60s, pending: 0, state: Connecting, last-heartbeat: never, last-mbeat: -1s ago, global: 0s ago
  web_1          |    --- End of inner exception stack trace ---
  
  web_1          | fail: Microsoft.AspNetCore.Server.Kestrel[13]
  web_1          |       Connection id "0HLKGR00VUQLM", Request id "0HLKGR00VUQLM:00000001": An unhandled exception was thrown by the application.
  web_1          | StackExchange.Redis.RedisConnectionException: No connection is available to service this operation: EVAL; SocketFailure on localhost:6379/Subscription, origin: Error, input-buffer: 0, outstanding: 0, last-read: 0s ago, last-write: 0s ago, unanswered-write: 1291s ago, keep-alive: 60s, pending: 0, state: Connecting, last-heartbeat: never, last-mbeat: -1s ago, global: 0s ago ---&gt; StackExchange.Redis.RedisConnectionException: SocketFailure on localhost:6379/Subscription, origin: Error, input-buffer: 0, outstanding: 0, last-read: 0s ago, last-write: 0s ago, unanswered-write: 1291s ago, keep-alive: 60s, pending: 0, state: Connecting, last-heartbeat: never, last-mbeat: -1s ago, global: 0s ago
  web_1          |    --- End of inner exception stack trace ---
  </code></pre>
  
  <p>How can I fix this problems? I don't know how to force ASP.NET Core app see Redis in Docker.</p>