2

I have created .NET core WebAPI project using Visual Studio. Added Docker support (to host it later on digitalocean), but i have faced a problem. When i run docker-compose - all works fine, but when i try to search in the mongoDB collectioni get next error:

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()" }] }.'

It seems like docker container is unable to connect to anything outside it, but i am new to docker and several days of googling resulted in nothing.

MongoDB is located on same machine as WebAPI. If Dockerfile or docker-compose data is needed, i will provide it.

Please, help me understand what i have missed and how to configure docker project properly to work with mongoDb and to keep this working even after publishing to linux server. Thank you in advance.

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"
4
  • Please provde your dockerfile and docker-compose, this is very important information ;) Commented Jun 12, 2018 at 18:16
  • I`ve updated the original post. Thank you. Commented Jun 12, 2018 at 18:43
  • Your MongoDb is running on a container as well? Commented Jun 12, 2018 at 19:05
  • No, my mongoDB running as a service on windows. And same situation planned on the server. Container with webapi and separately installed mongodb Commented Jun 12, 2018 at 20:01

1 Answer 1

1
EndPoint : "127.0.0.1:27017"

From a docker container's perspective, this means "in this container".

You should publish the container's 27017 port to the host IP, then configure your web container to use that socket. It's important that you can't use 127.0.0.1 as IP, bacause every container has this loopback address. Use your local network IP instead. You can read more about networking here: https://docs.docker.com/config/containers/container-networking/

Or you should create a bridge network and connect the containers to it, and reference the containers by their names (like mymongo:27017) using automatic service discovery. https://docs.docker.com/docker-cloud/apps/service-links/#discovering-containers-on-the-same-service-or-stack

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer. But what if i want my mongodb to be run not as a docker container but as a service on the same machine? P.s. As i understand from your unswer for docker it is problem to connect other apps on localhost? for example if my mongodb will be on some other address (example)192.168.1.67 it will work?
upd: I used my local network ip and it works, thank you. But i am not sure that this is good solution for digitalocean server. Making DB accessible over internet and using global ip to access it - not the best idea for me..
No, it is not. But there is nothing about Digital Ocean in the original question ;)
Perhaps you should use my second solution and create a bridge network. Or use (now deprecated) “link” feature. Maybe this helps: medium.com/@jmarhee/…
You helped me a lot when explained where is the problem. I havent knew that container looks inside itself when referenced to 127.0.0.1. Thank you once more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.