Search code examples
docker.net-coreasp.net-core-webapidocker-containerdocker-network

.NET Core in Docker container timeouts when querying the MariaDB Server on the host network


We are running a .NET Core 7 application in a Docker container on Linux Ubuntu. When querying the MariaDb server on the host network, it works fine for 1-2 requests and then it times out. The first 1-2 requests take unusually long.

  • The following configured connection-string works, the host RBDMS server can be reached (data is seeded):

    Server=host.docker.internal,3306;Database=xxxx;User ID=xxxx;Password=xxxx;Max Pool Size=200;
    
  • This is how we start the container

    docker run -d --restart unless-stopped -p 5055:80 --add-host host.docker.internal:host-gateway --name xx-container xx
    

I have read that maybe the docker container is doing unnecessary DNS requests. But I think that should be solved by accessing the hosts network as above.

The application's Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build-env
WORKDIR /App

COPY . ./

RUN dotnet restore

RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /App
COPY --from=build-env /App/out .
ENV DOTNET_EnableDiagnostics=0
ENTRYPOINT ["dotnet", "xx.dll"]

I tried running the container with --network=host and use localhost in the connection string, but that disables my configured ports.


Solution

  • The issue was resolved by correcting the db connection strings as following:

    The original connection string:

    Server=x.x.x.x,3306;Database=xx;User ID=xx;Password=xx;Max Pool Size=50;connect timeout=100;

    The new connection string:

    server=host.docker.internal;Port=3306;Database=x;uid=stingray;Password=x;Max Pool Size=50;connect timeout=100;