如何排查Windows 10上VS .NET Core Docker容器连接超时问题
.NET 6 MVC项目Docker Debug模式启动超时问题
我是Docker/容器新手,了解到Docker for Windows可配合Visual Studio通过DockerFile使用。创建默认.NET 6 MVC项目,IIS Express模式运行正常,但切换到Docker模式时,构建成功但浏览器打开应用超时。VS先弹出“远程操作耗时超出预期”,随后提示“与XXX.XX.XXX.XXX:XXXX的网络连接已丢失,调试将中止”。确定后Chrome打开http://localhost:0/,关闭防火墙也无效。
生成的默认DockerFile代码
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"]
排查过程
- 拉取其他示例镜像(如markheath/samplewebapp:windows和kgpkgp/samplewebapp:latest),用命令
docker run -d -p 8081:80 --name ContainerName ImageName:TagName ping -t localhost映射端口运行,可通过http://localhost:8081/正常访问,排除网络和防火墙问题。 - Docker Desktop中,testingdocker镜像存在,但testingDocker容器已停止,状态为“EXITED (4294967295)”,无“在浏览器中打开”选项。
- 命令行基于现有镜像创建新容器(
docker run -d -p 8083:80....),容器运行成功且出现“在浏览器中打开”选项,但点击后http://localhost:8083/加载失败,提示“localhost took too long to respond”(ERR_CONNECTION_TIMED_OUT)。
补充发现
切换到Release模式而非Debug模式时,项目可正常在Docker中运行并在浏览器打开,但Release模式无法调试代码,演示中Debug模式是支持单步调试的。尝试将DockerFile中的“Release”改为“Debug”,无效果。
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 ==========
内容的提问来源于stack exchange,提问作者J L
相关产品推荐
相关产品推荐

