c# - Webapi 在 docker-compose 中看不到 MongoDB

标签 c# mongodb docker .net-core docker-compose

我正在运行 docker-compose 文件,其中包含我的 WebApi 和 MongoDB 我已经创建了以下 docker-compose 文件,但是当我运行它时,我无法执行任何请求。

这是我的 docker 撰写文件:

version: '3.1'
services:
mongo:
    image: mongo
    restart: always
    environment:
      MONGODB_USER: "admin"
      MONGODB_DATABASE: "BooksDB"
      MONGODB_PASS: "pass"
    ports:
      - 27017:27017
mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    depends_on:
      - mongo
webapi:
    build: .
    restart: always
    ports:
      - 5000:80
    environment:
      MongoDB__Host: mongo
    depends_on:
    - mongo

我在 docker 容器日志中收到以下错误:

fail: Microsoft.AspNetCore.Server.Kestrel[13]
  Connection id "0HLPSARCVM003", Request id "0HLPSARCVM003:00000001": An unhandled exception was thrown by the  application.
System.TimeoutException: A timeout occured after 30000ms selecting a   server using CompositeServerSelector{ Selectors =   MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector,   LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 }   }. Client view of cluster state is { ClusterId : "1", ConnectionMode :   "Automatic", Type : "Unknown", State : "Disconnected", Servers : [{   ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/localhost:27017" }",     EndPoint: "Unspecified/localhost:27017", State: "Disconnected", Type:       "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException:        An exception occurred while opening a connection to the server. --->      System.Net.Sockets.SocketException: Cannot assign requested address
     at System.Net.Sockets.Socket.BeginConnectEx(EndPoint remoteEP,      Boolean flowContext, AsyncCallback callback, Object state)
     at System.Net.Sockets.Socket.UnsafeBeginConnect(EndPoint    remoteEP, AsyncCallback callback, Object state, Boolean flowContext)
     at System.Net.Sockets.Socket.BeginConnect(EndPoint remoteEP, AsyncCallback callback, Object state)

在 System.Net.Sockets.Socket.ConnectAsync(EndPoint RemoteEP) 在 MongoDB.Driver.Core.Connections.TcpStreamFactory.ConnectAsync(Socket 套接字、EndPoint endPoint、CancellationToken CancellationToken) 在 MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateStreamAsync(EndPoint endPoint,CancellationToken CancellationToken) 在 MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken CancellationToken) --- 内部异常堆栈跟踪结束 ---

你知道我应该在那些 docker-compose 文件中更改什么吗?

最佳答案

问题是您在 webapi 配置中引用了 localhost 。请将其替换为 mongoDB 的 docker 主机名,在本例中为 mongo:27017

关于c# - Webapi 在 docker-compose 中看不到 MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57997583/

相关文章:

c# - 富文本框有焦点时如何打开大写锁定

c# - 带有字体大小的 OpenXML 奇怪行为

javascript - 如何通过 Promise 重用 mongodb 连接

python - 怎么说......当字段是数字时匹配......在mongodb中?

c# - 如何比较不同尺寸的图像

node.js - 在多个集合中搜索 mongoose

python - 在 Conda UnsatisfiableError 消息中找不到冲突

python - 如何在docker容器中连接外部mysql服务器

Java + Docker : Different datasource url

c# - 获取 Nopcommerce 中的图片名称