title: Connecting to a mongodb thats within a Docker container
body: <p>I am trying to deploy my web api to a docker container. I am able to get the service running in docker but anything related to making a database call it falls over. I'm not entirely sure why. Here is my code:</p>
  <p><strong>Dockerfile_local</strong></p>
  <pre><code>FROM mcr.microsoft.com/dotnet/core/sdk:3.1.302-alpine3.12 as builder
  EXPOSE 27017
  
  COPY . .
  
  RUN dotnet publish --configuration Release MembershipApi/MembershipApi.csproj -o /app
  
  FROM mcr.microsoft.com/dotnet/core/aspnet:3.1.8-alpine3.12
  
  WORKDIR /app
  
  COPY --from=builder /app .
  
  CMD [&quot;dotnet&quot;, &quot;MembershipApi.dll&quot;]
  </code></pre>
  <p><strong>docker-compose-local.yml</strong></p>
  <pre><code>version: &quot;3&quot;
  services: 
      test:
          image: mcr.microsoft.com/dotnet/core/sdk:3.1.402-alpine3.12
          depends_on: 
              - membership-api
              - mongo
          entrypoint: [&quot;dotnet&quot;,&quot;test&quot;, &quot;/builds/MembershipApi.IntergrationTests/MembershipApi.IntergrationTests.csproj&quot;, &quot;--configuration&quot;, &quot;Release&quot;]
          volumes:
              - .:/builds
          
      membership-api:
          build:
              context: .
              dockerfile: Dockerfile_local
          depends_on:
              - mongo
          ports:
              - 9000:80
      mongo:
          image: mongo:4.0.16
          ports:
              - 27017
  </code></pre>
  <p><strong>Error</strong></p>
  <pre><code>membership-api_1  | warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
  membership-api_1  |       Failed to determine the https port for redirect.
  membership-api_1  | fail: Microsoft.AspNetCore.Server.Kestrel[13]
  membership-api_1  |       Connection id &quot;0HM3LNKLS24AA&quot;, Request id &quot;0HM3LNKLS24AA:00000002&quot;: An unhandled exception was thrown by the application.
  membership-api_1  | 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 : &quot;1&quot;, ConnectionMode : &quot;Automatic&quot;, Type : &quot;Unknown&quot;, State : &quot;Disconnected&quot;, Servers : [{ ServerId: &quot;{ ClusterId : 1, EndPoint : 
  &quot;Unspecified/localhost:27017&quot; }&quot;, EndPoint: &quot;Unspecified/localhost:27017&quot;, ReasonChanged: &quot;Heartbeat&quot;, State: &quot;Disconnected&quot;, ServerVersion: , TopologyVersion: , Type: &quot;Unknown&quot;, HeartbeatException: &quot;MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.
  membership-api_1  |  ---&gt; System.Net.Sockets.SocketException (99): Address not available
  membership-api_1  |    at System.Net.Sockets.Socket.BeginConnectEx(EndPoint remoteEP, Boolean flowContext, AsyncCallback callback, Object state)
  membership-api_1  |    at System.Net.Sockets.Socket.ConnectAsync(EndPoint remoteEP)
  membership-api_1  |    at MongoDB.Driver.Core.Connections.TcpStreamFactory.ConnectAsync(Socket socket, EndPoint endPoint, CancellationToken cancellationToken)
  membership-api_1  |    at MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)
  membership-api_1  |    at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
  membership-api_1  |    --- End of inner exception stack trace ---
  membership-api_1  |    at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
  membership-api_1  |    at MongoDB.Driver.Core.Servers.ServerMonitor.InitializeConnectionAsync(CancellationToken cancellationToken)
  membership-api_1  |    at MongoDB.Driver.Core.Servers.ServerMonitor.HeartbeatAsync(CancellationToken cancellationToken)&quot;, LastHeartbeatTimestamp: &quot;2020-10-21T12:31:43.5254072Z&quot;, LastUpdateTimestamp: &quot;2020-10-21T12:31:43.5254077Z&quot; }] }.
  membership-api_1  |    at MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException(IServerSelector selector, ClusterDescription description)
  membership-api_1  |    at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedHelper.HandleCompletedTask(Task completedTask)
  membership-api_1  |    at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChanged(IServerSelector selector, ClusterDescription description, Task descriptionChangedTask, TimeSpan timeout, CancellationToken cancellationToken)
  membership-api_1  |    at MongoDB.Driver.Core.Clusters.Cluster.SelectServer(IServerSelector selector, CancellationToken cancellationToken)
  membership-api_1  |    at MongoDB.Driver.MongoClient.AreSessionsSupportedAfterServerSelection(CancellationToken cancellationToken)
  membership-api_1  |    at MongoDB.Driver.MongoClient.AreSessionsSupported(CancellationToken cancellationToken)
  membership-api_1  |    at MongoDB.Driver.MongoClient.StartImplicitSession(CancellationToken cancellationToken)
  membership-api_1  |    at MongoDB.Driver.OperationExecutor.StartImplicitSession(CancellationToken cancellationToken)
  membership-api_1  |    at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSession[TResult](Func`2 func, CancellationToken cancellationToken)
  membership-api_1  |    at MongoDB.Driver.MongoCollectionImpl`1.FindSync[TProjection](FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
  membership-api_1  |    at MongoDB.Driver.FindFluent`2.ToCursor(CancellationToken cancellationToken)
  membership-api_1  |    at MongoDB.Driver.IAsyncCursorSourceExtensions.SingleOrDefault[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)
  membership-api_1  |    at MongoDB.Driver.IFindFluentExtensions.SingleOrDefault[TDocument,TProjection](IFindFluent`2 find, CancellationToken cancellationToken)
  membership-api_1  |    at MembershipApi.Data.PaymentRepo.GetBalanceById(Int32 id) in /MembershipApi/Data/PaymentRepo.cs:line 23
  membership-api_1  |    at MembershipApi.Controllers.PaymentController.GetBalance(Int32 id) in /MembershipApi/Controllers/PaymentController.cs:line 24
  membership-api_1  |    at lambda_method(Closure , Object , Object[] )
  membership-api_1  |    at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
  membership-api_1  |    at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)     
  membership-api_1  |    at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
  membership-api_1  |    at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
  membership-api_1  |    at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
  membership-api_1  | --- End of stack trace from previous location where exception was thrown ---
  membership-api_1  |    at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
  membership-api_1  |    at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
  membership-api_1  |    at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
  membership-api_1  | --- End of stack trace from previous location where exception was thrown ---
  membership-api_1  |    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeFilterPipelineAsync&gt;g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)  
  membership-api_1  |    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeAsync&gt;g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
  membership-api_1  |    at Microsoft.AspNetCore.Routing.EndpointMiddleware.&lt;Invoke&gt;g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
  membership-api_1  |    at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
  membership-api_1  |    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
  </code></pre>