mongodb - Docker容器无法连接MongoDB

标签 mongodb docker networking .net-core asp.net-web-api2

我已经使用Visual Studio创建了.NET核心WebAPI项目。添加了Docker支持(以便稍后在digitalocean上托管),但是我遇到了一个问题。
当我运行docker-compose-一切正常时,但是当我尝试在mongoDB collection中搜索时,出现下一个错误:

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 : "127.0.0.1:27017" }", EndPoint: "127.0.0.1:27017", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: Connection refused 127.0.0.1:27017
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MongoDB.Driver.Core.Connections.TcpStreamFactory.<ConnectAsync>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MongoDB.Driver.Core.Connections.TcpStreamFactory.<CreateStreamAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MongoDB.Driver.Core.Connections.BinaryConnection.<OpenHelperAsync>d__48.MoveNext()
   --- End of inner exception stack trace ---
   at MongoDB.Driver.Core.Connections.BinaryConnection.<OpenHelperAsync>d__48.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MongoDB.Driver.Core.Servers.ServerMonitor.<HeartbeatAsync>d__27.MoveNext()" }] }.'

似乎Docker容器无法连接到其外部的任何东西,但是我对Docker并不陌生,并且经过数天的谷歌搜索没有任何结果。

MongoDB与WebAPI位于同一台计算机上。如果需要Dockerfile或docker-compose数据,我会提供。

请帮助我了解我错过了什么,以及如何正确配置docker项目以与mongoDb一起使用,甚至在发布到linux服务器后也能使其正常工作。
先感谢您。

Dockerfile:
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY *.sln ./
COPY Temp_Server/Temp_Server.csproj Temp_Server/
RUN dotnet restore
COPY . .
WORKDIR /src/Temp_Server
RUN dotnet build -c Release -o /app

FROM build AS publish
RUN dotnet publish -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Temp_Server.dll"]

docker-compose.ci.build.yml:
version: '3'

services:
  ci-build:
    image: microsoft/aspnetcore-build:1.0-2.0
    volumes:
      - .:/src
    working_dir: /src
    command: /bin/bash -c "dotnet restore ./Temp_Server.sln && dotnet publish ./Temp_Server.sln -c Release -o ./obj/Docker/publish"

docker-compose.yml:
version: '3'

services:
  temp_server:
    image: temp_server
    build:
      context: .
      dockerfile: temp_Server/Dockerfile

docker-compose.override.yml:
version: '3'

services:
  temp_server:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
    ports:
      - "80"

最佳答案

EndPoint : "127.0.0.1:27017"

从Docker容器的 Angular 来看,这意味着“在此容器中”。

您应该将容器的27017端口发布到主机IP,然后将Web容器配置为使用该套接字。重要的是您不能使用127.0.0.1作为IP,因为每个容器都有此回送地址。请改用您的本地网络IP。您可以在此处阅读有关联网的更多信息:https://docs.docker.com/config/containers/container-networking/

或者,您应该创建一个网桥网络并将其连接到容器,然后使用自动服务发现通过容器的名称(例如mymongo:27017)来引用这些容器。
https://docs.docker.com/docker-cloud/apps/service-links/#discovering-containers-on-the-same-service-or-stack

关于mongodb - Docker容器无法连接MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50823368/

相关文章:

database - 如何创建数据库的 MongoDB 转储?

docker - 如何在Docker撰写文件上设置MySQL和phpmyadmin

docker - 如何在 nginx alpine docker 镜像中启用 native 模块

c - 服务器-客户端模型问题

Java 服务器套接字。为什么IP地址是0.0.0.0,但我仍然可以远程连接?

MongoDB : Indexes order and query order must match?

javascript - 如何使用 Javascript 保存地理围栏?

javascript - 聚合嵌套数组中的数据

docker - openssh-server无法在Docker容器中启动

security - 如何唯一标识一个网络?