【问题标题】:How can I dockerize ASP.NET Core app and Redis together?如何将 ASP.NET Core 应用程序和 Redis 一起 dockerize?
【发布时间】:2019-02-12 12:09:14
【问题描述】:

我正在使用 Docker 并尝试连接 redis 和 Web 应用程序。这是我的 docker-compose 文件:

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"

这是我的连接字符串:

"ConnectionStrings": {
    "redis": "localhost:6379,abortConnect=False"
  },

Dockerfile 看起来像这样:

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"]

在 Startup.cs 中连接到 Redis:

services.AddDistributedRedisCache(option =>
{
    option.Configuration = Configuration.GetConnectionString("redis");
}); 

要连接到 docker,我会在 cmd 中编写下一个命令:

docker-compose build
docker-compose up

此命令运行良好,但在我转到“localhost:9901”后,我在控制台中收到下一个错误

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 ---> 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 ---> 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 ---

我该如何解决这个问题?我不知道如何强制 ASP.NET Core 应用在 Docker 中看到 Redis。

【问题讨论】:

    标签: c# docker asp.net-core redis docker-compose


    【解决方案1】:

    你需要改变两件事:

    1:Docker-compose 文件应该更新并且应该有用于容器通信的链接。

    version: '3.7'
    
    services:  
      web:
        image: redistest:latest
        depends_on:
          - "redis_image"
        build:
          context: .
          dockerfile: Dockerfile
        ports:
          - "9901:80"
        links:
          - "redis_image"
    
      redis_image:
        image: redis
        container_name: cache
        ports:
          - "6379:6379"
    

    2:应该更新 ConnectionStrings。代替 localhost 使用 redis_image 的服务名称,如下所示

    "ConnectionStrings": {
        "redis": "redis_image:6379,abortConnect=False"
      },
    

    告诉我结果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-02
      • 2021-06-06
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      相关资源
      最近更新 更多