Docker:应用程序可以通过docker-up来正常工作,但是如何通过Visual Studio来运行它并进行debugging?

我有几个项目,他们必须在独立的容器中运行,我也有一些共享库,应该是buit。 我发现以下文章如何做到这一点。 我只会显示一个项目的docker文件,因为它们是相同的:

FROM microsoft/aspnetcore:2.0 AS base WORKDIR /app EXPOSE 80 FROM microsoft/aspnetcore-build:2.0 AS builder WORKDIR /src COPY *.sln ./ COPY Web/Web.csproj Web/ RUN dotnet restore COPY . . WORKDIR /src/Web RUN dotnet build -c Debug -o /app FROM builder AS publish RUN dotnet publish -c Debug -o /app FROM base AS production WORKDIR /app COPY --from=publish /app . ENTRYPOINT ["dotnet", "Web.dll"] 

所以,你可以看到使用多级build筑。 如果我使用docker-compose up那么一切正常。 接下来,我试图通过Visual Studio运行它,我看到Output窗口中的所有步骤,但最后我得到以下错误:

目标进程退出,但没有引发CoreCLR启动事件。 确保目标进程被configuration为使用.NET Core。 如果目标进程不在.NET Core上运行,则可能会出现这种情况。 程序'[13] dotnet'退出代码145(0x91)。 该程序已退出,代码为145(0x91)。

但是,现在如何debugging应用程序呢? 这是github 回购的链接。

PS。 Tarun是VS生成的默认docker文件

 FROM microsoft/aspnetcore:2.0 ARG source WORKDIR /app EXPOSE 80 COPY ${source:-obj/Docker/publish} . ENTRYPOINT ["dotnet", "Web.dll"] 

TL; DR;

所以我安装了VS 2017,并且深入了解了这里所发生的一切。 看了下你的项目的构build过程

docker-compose -f“C:\ Users \ tarlabs \ Desktop \ AspNetCoreMultiProject \ docker-compose.yml”-f“C:\ Users \ tarlabs \ Desktop \ AspNetCoreMultiProject \ docker-compose.override.yml”-f“C: \ Users \ tarlabs \ Desktop \ AspNetCoreMultiProject \ obj \ Docker \ docker-compose.vs.debug.g.yml“-p dockercompose15184637154516733497 kill

泊坞窗,compose.override.yml

 version: '3' services: web: environment: - ASPNETCORE_ENVIRONMENT=Development ports: - "80" api: environment: - ASPNETCORE_ENVIRONMENT=Development ports: - "80" 

这不是很有趣。

泊坞窗,compose.vs.debug.g.yml

 version: '3' services: api: image: api:dev build: args: source: obj/Docker/empty/ environment: - DOTNET_USE_POLLING_FILE_WATCHER=1 - NUGET_FALLBACK_PACKAGES=/root/.nuget/fallbackpackages volumes: - C:\Users\tarlabs\Desktop\AspNetCoreMultiProject:/app - C:\Users\tarlabs\vsdbg:/remote_debugger:ro - C:\Users\tarlabs\.nuget\packages\:/root/.nuget/packages:ro - C:\Program Files\dotnet\sdk\NuGetFallbackFolder:/root/.nuget/fallbackpackages:ro entrypoint: tail -f /dev/null labels: com.microsoft.visualstudio.debuggee.program: "dotnet" com.microsoft.visualstudio.debuggee.arguments: " --additionalProbingPath /root/.nuget/packages --additionalProbingPath /root/.nuget/fallbackpackages bin/Debug/netcoreapp2.0/Api.dll" com.microsoft.visualstudio.debuggee.workingdirectory: "/app" com.microsoft.visualstudio.debuggee.killprogram: "/bin/bash -c \"if PID=$$(pidof -x dotnet); then kill $$PID; fi\"" web: image: web:dev build: args: source: obj/Docker/empty/ environment: - DOTNET_USE_POLLING_FILE_WATCHER=1 - NUGET_FALLBACK_PACKAGES=/root/.nuget/fallbackpackages volumes: - C:\Users\tarlabs\Desktop\AspNetCoreMultiProject:/app - C:\Users\tarlabs\vsdbg:/remote_debugger:ro - C:\Users\tarlabs\.nuget\packages\:/root/.nuget/packages:ro - C:\Program Files\dotnet\sdk\NuGetFallbackFolder:/root/.nuget/fallbackpackages:ro entrypoint: tail -f /dev/null labels: com.microsoft.visualstudio.debuggee.program: "dotnet" com.microsoft.visualstudio.debuggee.arguments: " --additionalProbingPath /root/.nuget/packages --additionalProbingPath /root/.nuget/fallbackpackages bin/Debug/netcoreapp2.0/Web.dll" com.microsoft.visualstudio.debuggee.workingdirectory: "/app" com.microsoft.visualstudio.debuggee.killprogram: "/bin/bash -c \"if PID=$$(pidof -x dotnet); then kill $$PID; fi\"" 

几件有趣的事情

  • 我们定义的ENTRYPOINT在debugging时没有什么区别,因为它被VS用tail -f /dev/null覆盖
  • com.microsoft.visualstudio.debuggee.arguments具有pathbin/Debug/netcoreapp2.0/Web.dll
  • debugging工作目录始终设置为/app使用com.microsoft.visualstudio.debuggee.workingdirectory
  • 卷装入C:\Users\tarlabs\Desktop\AspNetCoreMultiProject:/app

看卷装载C:\Users\tarlabs\Desktop\AspNetCoreMultiProject:/app ,我就像哇! 任何你在你的Docker文件中的/app文件夹中的东西,都会被这个mount所覆盖。 所以,无论你是build立和放置文件,还是不做任何事情都不会有所作为。

现在我走进容器,并意识到Web.dll是内部/app/Web/bin/Debug/netcoreapp2.0/Web.dll但debugging器期望它在/app/bin/Debug/netcoreapp2.0/Web.dll 。 在每一个环境中寻找后,我无法find任何地方的path。

然后我玩了一个新的项目。 添加一个项目与Docker支持,然后添加另一个项目与docker支持。 这给了我一个docker-compose.yml的提示

 version: '3' services: webapplication1: image: webapplication1 build: context: ./WebApplication1 dockerfile:Dockerfile webapplication2: image: webapplication2 build: context: ./../WebApplication2 dockerfile: Dockerfile 

这给了我一个提示,即dynamicdocker-compose.vs.debug.g.yml文件将根据docker-compose.yml中给出的上下文来进行卷挂载。 现在看你的项目。

泊坞窗,compose.yml

 version: '3' services: web: image: web build: context: . dockerfile: Web/Dockerfile api: image:api build: context: . dockerfile: Api/Dockerfile 

由于上下文是. 卷装入生成为

 - C:\Users\tarlabs\Desktop\AspNetCoreMultiProject:/app 

为了纠正我们更新我们docker-compose.yml

 version: '3' services: web: image: web build: context: ./Web dockerfile: Dockerfile api: image:api build: context: ./Api dockerfile: Dockerfile 

接下来我们的Dockerfile做了太多的事情,VSdebugging器只是忽略。 所以你只需要在你的Dockerfile两行来进行debugging就可以正常工作

 FROM microsoft/aspnetcore:2.0 AS base WORKDIR /app 

rest你所做的任何事情只是被卷装扔掉。 所以在debugging时没有意义。 您可以使用多阶段构build方法来部署到生产,但不能用于debugging。 在你的项目debugging中做出这两个更改后,开始为我工作

调试工作