我使用Visual Studio创建了.NET核心WebAPI项目。添加了Docker支持(稍后在digitalocean上托管它),但我遇到了一个问题。 当我运行docker-compose时 - 一切正常,但是当我尝试在mongoDB集合中搜索时会出现下一个错误:
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"]
搬运工-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"
搬运工-compose.yml:
version: '3'
services:
temp_server:
image: temp_server
build:
context: .
dockerfile: temp_Server/Dockerfile
搬运工-compose.override.yml:
version: '3'
services:
temp_server:
environment:
- ASPNETCORE_ENVIRONMENT=Development
ports:
- "80"
答案 0 :(得分:1)
EndPoint : "127.0.0.1:27017"
从docker容器的角度来看,这意味着“在这个容器中”。
您应该将容器的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