0

I cannot seem to figure out how to connect to my atlas db from .net core 5 web api project running inside a docker container. When I run the application locally with kestrel, then I am able to connect and query data without a problem. The docker runtime image is aspnet:5.0-alpine. I am using a self signed dev cert for SSL and HTTPS configuration which is working as intended. I am copying the .pfx file into the docker container. I believe this problem is related to the TLS/SSL certification process that mongodb atlas requires. I am using the MongoDB.Driver version 2.13.1. I have tried to load the SSL cert and include it in the client settings before connecting to the db, but that failed as well. I have also searched a lot on the mongodb community forums and online but have only found posts that describe the problem without any solutions. Any help would be much appreciated.

Below is my docker file.

FROM mcr.microsoft.com/dotnet/sdk:5.0-alpine AS build-env
WORKDIR /app

COPY ["Hammerhead.Common/Hammerhead.Common.csproj", "Hammerhead.Common/"]
COPY ["Hammerhead.Data/Hammerhead.Data.csproj", "Hammerhead.Data/"]
COPY ["Hammerhead.DataProvider/Hammerhead.DataProvider.csproj", "Hammerhead.DataProvider/"]
COPY ["Hammerhead.Services/Hammerhead.Services.csproj", "Hammerhead.Services/"]
COPY ["Hammerhead.App/Hammerhead.App.csproj", "Hammerhead.App/"]

RUN dotnet restore "Hammerhead.App/Hammerhead.App.csproj"

COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:5.0-alpine as runtime

# SQL culture fix
RUN apk add icu-libs
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false

#ENV ASPNETCORE_URLS_ http://+:5000;https://+:5001

WORKDIR /app
COPY --from=build-env /app/out .

COPY ./Hammerhead.pfx ./

ENTRYPOINT ["dotnet", "Hammerhead.App.dll"]

Below if the docker-compose

version: "3"
services:
    rabbitmq:
        image: masstransit/rabbitmq:latest
        ports:
          - "5672:5672"
          - "15672:15672"
    seq:
        image: datalust/seq:latest
        ports:
          - 8081:80
          - 5341:5341
        environment:
          ACCEPT_EULA: "Y"
    trading-app:
        build:
            context: .
            dockerfile: Dockerfile-App
        ports:
            - 5000:80
            - 5001:443
        depends_on:
            - rabbitmq
            - seq
        environment:
            ASPNETCORE_URLS: "https://+;http://+"
            ASPNETCORE_ENVIRONMENT: "Development"
            ASPNETCORE_Kestrel__Certificates__Default__Password: "password"
            ASPNETCORE_Kestrel__Certificates__Default__Path: "./Hammerhead.pfx"
            ASPNET_CORE_HTTPS_PORT: 5001
            Serilog__WriteTo__1__Args__serverURL: "http://seq:5341"
            RABBIT_HOSTNAME: "rabbitmq"

Below is the MongoDB.Driver code that I am using to connect.

string connectionString = "";
MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
settings.SslSettings = new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 };
var mongoClient = new MongoClient(settings);

I have also tested many iterations of the approve code and was unsuccessful. Also, I do not get any errors when trying to retrieve data. It returns an empty set.

2
  • Please share more details about your Dockerfile/docker-compose.yaml, and what kind of error are you getting while trying to connect to mongodb? Sep 23, 2021 at 9:40
  • Thanks for the response. I have add the Dockerfile, docker-compose.yaml and the c# code using the MongoDB.Driver Sep 24, 2021 at 12:43

0

Your Answer

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

Browse other questions tagged or ask your own question.