Windows docker container with .NET 6 API integration tests become extremely slow

Viewed 65

When we run some integration tests in a pipeline inside a docker container, we get timeouts or they are extremely slow. We think it's related to using localhost inside the tests. This only happens on our build agent.

If we run the integration tests locally in docker we don't get the timeouts. So I have a feeling this is related to the docker engine settings or version. Rather then the container.

If we run the tests outside the docker container they run fast on both machines.

To run our integration tests we use WebApplicationFactory from Microsoft.AspNetCore.Mvc.Testing. Which creates a HttpClient with base address: http://localhost. We already tried with a different base address using: WebApplicationFactoryClientOptions, but no success.

We feel like on every request an additional 15 seconds is added.

Does anyone have an idea where to start searching?

Command to start the container

docker run --name "OurName" -d -t -w c:/workspace/ -v d:/workspace/project/:c:/workspace ****.azurecr.io/win_dotnet:6.0  cmd /c ping -t localhost   > NUL

Command we use to run inside container

docker exec -i "OurName" pwsh.exe '-Command' "{replace with commands below}"

Commands we run inside the container

dotnet build --configuration Debug
dotnet test --no-build --filter 'Category=Integration' 

Sample Docker image

# escape=`

# Start from servercore:2022
ARG REPO=mcr.microsoft.com/windows/servercore
FROM $REPO:ltsc2022

ARG PS_VERSION=7.2.4
ARG PS_PACKAGE_URL=https://github.com/PowerShell/PowerShell/releases/download/v${PS_VERSION}/PowerShell-${PS_VERSION}-win-x64.zip
ARG DOTNET_SDK_VERSION=6.0.301
ARG AZURE_CLI_VERSION=2.37.0

ENV `
    # DOTNET variables
    ASPNETCORE_URLS= `
    DOTNET_GENERATE_ASPNET_CERTIFICATE=false `
    DOTNET_NOLOGO=true `
    DOTNET_USE_POLLING_FILE_WATCHER=true `
    NUGET_XMLDOC_MODE=skip `
    POWERSHELL_DISTRIBUTION_CHANNEL=PSDocker-DotnetSDK-WindowsServerCore-ltsc2022 `
    PSCORE="$ProgramFiles\PowerShell\pwsh.exe" `
    POWERSHELL_TELEMETRY_OPTOUT="1"` 
    PSModuleAnalysisCachePath="C:\Users\Public\AppData\Local\Microsoft\Windows\PowerShell\docker\ModuleAnalysisCache"


# Add powershell, dotnet to path
RUN setx /M PATH "%ProgramFiles%\PowerShell\latest;%PATH%;%ProgramFiles%\dotnet;%ProgramFiles(x86)%\Microsoft SDKs\Azure\CLI2\wbin;"

# Install dotnet
RUN powershell -Command " `
        $ErrorActionPreference = 'Stop'; `
        $ProgressPreference = 'SilentlyContinue'; `
        `
        # Retrieve .NET SDK
        mkdir "$Env:ProgramFiles\dotnet";`
        Invoke-WebRequest -OutFile dotnet.zip https://dotnetcli.azureedge.net/dotnet/Sdk/$Env:DOTNET_SDK_VERSION/dotnet-sdk-$Env:DOTNET_SDK_VERSION-win-x64.zip; `
        tar -oxzf dotnet.zip -C "$Env:ProgramFiles\dotnet"; `
        Remove-Item -Force dotnet.zip; `
        `
"

# Install AZ cli
RUN powershell -Command " `
        Invoke-WebRequest -Uri https://azcliprod.blob.core.windows.net/msi/azure-cli-$Env:AZURE_CLI_VERSION.msi -OutFile .\AzureCLI.msi; `
        Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; `
        Remove-Item -Path .\AzureCLI.msi; `
        `
        az version `
"

# Install powershell core
RUN powershell -Command "`
    Write-host "Verifying valid Version..."; `
    if (!($env:PS_VERSION -match '^\d+\.\d+\.\d+(-\w+(\.\d+)?)?$' )) { `
        throw ('PS_Version ({0}) must match the regex "^\d+\.\d+\.\d+(-\w+(\.\d+)?)?$"' -f $env:PS_VERSION) `
    } `
    $ProgressPreference = 'SilentlyContinue'; `
    if($env:PS_PACKAGE_URL_BASE64){ `
        Write-host "decoding: $env:PS_PACKAGE_URL_BASE64" ;`
        $url = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($env:PS_PACKAGE_URL_BASE64)) `
    } else { `
        Write-host "using url: $env:PS_PACKAGE_URL" ;`
        $url = $env:PS_PACKAGE_URL `
    } `
    Write-host "downloading: $url"; `
    [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12; `
    Invoke-WebRequest -Uri $url -outfile /powershell.zip -verbose; `
    Expand-Archive powershell.zip -DestinationPath  "$Env:ProgramFiles\PowerShell\latest" `
"

# intialize powershell module cache
RUN pwsh `
        -NoLogo `
        -NoProfile `
        -Command " `
          $stopTime = (get-date).AddMinutes(15); `
          $ErrorActionPreference = 'Stop' ; `
          $ProgressPreference = 'SilentlyContinue' ; `
          while(!(Test-Path -Path $env:PSModuleAnalysisCachePath)) {  `
            Write-Host "'Waiting for $env:PSModuleAnalysisCachePath'" ; `
            if((get-date) -gt $stopTime) { throw 'timout expired'} `
            Start-Sleep -Seconds 6 ; `
          }"


# Install AZ iot
RUN az extension add --name azure-iot

General info:

Docker engine version on build server:20.10.9, build 591094d 
Docker for windows on dev machine: 20.10.17, build 100c701 
Dotnet version:6.0.301
0 Answers
Related