How do I troubleshoot Visual Studio .net core Windows 10 Docker containers with error "localhost took too long to respond" (ERR_CONNECTION_TIMED_OUT)?

Viewed 34

New to Docker / containers. Everything I've read and watched online shows that Docker for Windows "just works" with Visual Studio by adding the DockerFile to the project, and away it goes. I have a default/generated MVC .Net 6 (core) application (I added no logic of my own), and it runs just fine under the "IIS Express" mode. When I select the "Docker" mode, it appears to build ok, but then it times out when it tries to open the app in the browser. First it pops up a message in Visual Studio saying, "A remote operation is taking longer than expected", then another message pops up in Visual Studio saying, "The network connection to XXX.XX.XXX.XXX:XXXX has been lost. Debugging will be aborted". When I click OK, it opens a browser window (Chrome) to url http://localhost:0/. Turning off the firewall didn't help.

I've placed the "Container Tools" output from VS at the end of this description.

I left the DockerFile code itself as-is from what is generated by VS:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["testingDocker.csproj", "."]
RUN dotnet restore "./testingDocker.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "testingDocker.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "testingDocker.csproj" -c Release -o /app/publish 
#/p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "testingDocker.dll"]

I have pulled a couple other sample images from Docker (e.g., "markheath/samplewebapp:windows" and "kgpkgp/samplewebapp:latest"), ran each of them, mapping the desired ports, e.g.,

docker run -d -p 8081:80 --name ContainerName ImageName:TagName ping -t localhost

...and then opened them in the browser successfully, e.g.,

http://localhost:8081/

I have used various host ports with success for these pulled images, so I think I can rule out network and firewall issues.

When I look at the Docker Desktop display, the "testingdocker" image is there, and the "testingDocker" container is there, but stopped with a status of "EXITED (4294967295)". The "Open with browser" option does not appear under the ellipsis button. When I try to create a new container from the existing image using the command line (docker run -d -p 8083:80....), the container successfully is created and runs, and the "Open with browser" option is available; when I select it, the browser opens to http://localhost:8083/ but fails to load, displaying message "localhost took too long to respond" (ERR_CONNECTION_TIMED_OUT). I've second-guessed (and third-guessed, etc) everything. Any advice on what might be wrong here?

EDIT: I randomly discovered that if I ran it in Release mode (as opposed to Debug mode), it behaves as expected (i.e., runs in the browser, runs in Docker), which is a great step forward. Of course, being in Release mode, I can't actually debug the code, whereas all of the demos out there show that you can be in Debug and step through the code. I'm still in need of getting it to run in Debug. I tried changing the keyword "Release" to "Debug" in the Dockerfile hoping that would be the issue, but it made no difference.

"Container Tools" output from VS:

========== Checking for Container Prerequisites ==========
Verifying that Docker Desktop is installed...
Docker Desktop is installed.
========== Verifying that Docker Desktop is running... ==========
Verifying that Docker Desktop is running...
Docker Desktop is running.
========== Verifying Docker OS ==========
Verifying that Docker Desktop's operating system mode matches the project's target operating system...
Docker Desktop's operating system mode matches the project's target operating system.
========== Pulling Required Images ==========
Checking for missing Docker images...
Pulling Docker images. To cancel this download, close the command prompt window.
docker pull mcr.microsoft.com/dotnet/aspnet:6.0
Docker images are ready.
========== Warming up container(s) for testingDocker ==========
Starting up container(s)...
docker build -f "C:\J\testingDocker\testingDocker\Dockerfile" --force-rm -t testingdocker:dev --target base  --label "com.microsoft.created-by=visual-studio" --label "com.microsoft.visual-studio.project-name=testingDocker" "C:\J\testingDocker\testingDocker" 
Sending build context to Docker daemon  8.199MB

Step 1/5 : FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
 ---> 1eaec0f2abb1
Step 2/5 : WORKDIR /app
 ---> Running in d3b72aeec68e
Removing intermediate container d3b72aeec68e
 ---> 3f573ca3d231
Step 3/5 : EXPOSE 8080
 ---> Running in a880a99ebd54
Removing intermediate container a880a99ebd54
 ---> 22255aa22f3f
Step 4/5 : LABEL com.microsoft.created-by=visual-studio
 ---> Running in 58b503e11d66
Removing intermediate container 58b503e11d66
 ---> 41c0305f94a5
Step 5/5 : LABEL com.microsoft.visual-studio.project-name=testingDocker
 ---> Running in 3dea4d690e3c
Removing intermediate container 3dea4d690e3c
 ---> d05c6888e64f
Successfully built d05c6888e64f
Successfully tagged testingdocker:dev

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
docker rm -f 2319c1e877821720d122cd45ee01c27cabf410acee68d41528e691a19e1bc8c5
2319c1e877821720d122cd45ee01c27cabf410acee68d41528e691a19e1bc8c5
docker run -dt -v "C:\Users\James\onecoremsvsmon\17.3.10630.1389:C:\remote_debugger:ro" -v "C:\J\testingDocker\testingDocker:C:\app" -v "C:\J\testingDocker\testingDocker:c:\src" -v "C:\Users\James\.nuget\packages\:c:\.nuget\fallbackpackages2" -v "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages:c:\.nuget\fallbackpackages" -e "ASPNETCORE_LOGGING__CONSOLE__DISABLECOLORS=true" -e "ASPNETCORE_ENVIRONMENT=Development" -e "DOTNET_USE_POLLING_FILE_WATCHER=1" -e "NUGET_PACKAGES=c:\.nuget\fallbackpackages2" -e "NUGET_FALLBACK_PACKAGES=c:\.nuget\fallbackpackages;c:\.nuget\fallbackpackages2" -P --name testingDocker_1 --entrypoint C:\remote_debugger\x64\msvsmon.exe testingdocker:dev /noauth /anyuser /silent /nostatus /noclrwarn /nosecuritywarn /nofirewallwarn /nowowwarn /fallbackloadremotemanagedpdbs /timeout:2147483646 /LogDebuggeeOutputToStdOut 
51a6d9e31decdc7adf618d8cadbe09ef26e3aa19c13a7d63f13353761a333f89
Container started successfully.
========== Finished ==========
1 Answers

You can try below methods:

  1. Check whether you have used networking security software. You can disable or close those application.

  2. You can use DebugDiag Tools to monitor devenv.exe and msvsmon.exe, and collect the dump files.

Related