简体   繁体   English

Webapi在docker-compose中看不到MongoDB

[英]Webapi doest not see MongoDB in docker-compose

Im running docker-compose file which contains my WebApi and MongoDB I already created following docker-compose file but when im running it I am not able to perform any request. 我正在运行包含我的WebApi和MongoDB的docker-compose文件,我已经在docker-compose文件之后创建了该文件,但是当我运行它时,我无法执行任何请求。

This is my docker compose file: 这是我的docker compose文件:

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

im receiving following error in docker container log: 我在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)

at System.Net.Sockets.Socket.ConnectAsync(EndPoint remoteEP) at MongoDB.Driver.Core.Connections.TcpStreamFactory.ConnectAsync(Socket socket, EndPoint endPoint, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken) --- End of inner exception stack trace --- 在MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateStreamAsync(EndPoint)的System.Net.Sockets.Socket.ConnectAsync(EndPoint remoteEP)在MongoDB.Driver.Core.Connections.TcpStreamFactory.ConnectAsync(Socket socket,EndPoint endPoint,CancellationToken cancellingToken)在MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancelleToken)处的endPoint,CancellationToken cancellingToken)-内部异常堆栈跟踪的结尾-

do you know what im supposed to change in those docker-compose file ? 你知道这些docker-compose文件中我应该改变什么吗?

The problem is that you're referring to localhost in your webapi config. 问题是您在webapi配置中引用了localhost Please replace that with the docker hostname of t he mongoDB, in this case mongo:27017 请用mongoDB的docker主机名替换它,在本例中为mongo:27017

The problem seems not to be with your docker-compose file, but with the code that you have written in the webapi service. 问题似乎不在于您的docker-compose文件,而是与您在webapi服务中编写的代码有关。 The log that you have published shows an exception regarding the 'localhost:27017' endpoint, and the webapi should not look to localhost for the MongoDB. 您已发布的日志显示有关“ localhost:27017”端点的异常,并且webapi不应将MongoDB移至localhost。 Instead try to use the environment variable that you set: 而是尝试使用您设置的环境变量:

    environment:
      MongoDB__Host: mongo

This means that the webapi should look to mongo:27017 instead of localhost:27017 . 这意味着webapi应该查找mongo:27017而不是localhost:27017 But don't hardcode mongo:27017, but instead read from the environment. 但是不要对mongo:27017进行硬编码,而是从环境中读取。 The reason is that each service you have defined gets it own IP and hostname on the bridge network. 原因是您定义的每个服务都在网桥网络上获得了自己的IP和主机名。 In fact there is no reason for you to expose the 27017 port in your docker-compose file, as the communication will flow directly on the bridge network between services. 实际上,您没有理由在docker-compose文件中公开27017端口,因为通信将直接在服务之间的桥接网络上流动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM