如何从 Visual Studio 调试在 Linux Docker 容器中运行的 .NET Core 应用程序 [英] How to debug a .NET Core application running in a Linux Docker container from Visual Studio

查看:18
本文介绍了如何从 Visual Studio 调试在 Linux Docker 容器中运行的 .NET Core 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自己手写的 Dockerfile/docker-compose 文件.我从命令行启动容器.现在我想将 Visual Studio 2017(不是 Visual Studio Code)附加到我的 Docker 应用程序中(基于 Linux 的)容器.这似乎应该是一项非常简单的任务,但我找不到有关如何执行此操作的任何信息.

I have my own hand written Dockerfile/docker-compose files. I start containers from command line. Now I want to attach Visual Studio 2017 (not Visual Studio Code) to my application inside a Docker (Linux-based) container. It seems it should a pretty easy task, but I can't find any information on how to do this.

我通读了指南在 Linux 或 OS X 上从 Visual Studio 仔细调试 .NET Core.起初它看起来像我需要的 - 关于如何远程调试在 Linux 中运行的 .NET Core 应用程序的描述.但它只讲述了故事的一部分——如何通过 SSH 进行调试.并且只提到了 Docker,但没有说明如何在 Docker 中远程调试应用程序.

I read through the guide Off-road Debugging of .NET Core on Linux or OS X from Visual Studio carefully. At first it looked like what I needed - a description on how to remotely debug a .NET Core application running in Linux. But it only tells a part of the story - how to debug via SSH. And just mentions Docker, but it says nothing to how to remotely debug an application inside Docker.

我想这里不应该有太多关于 Docker 的内容,它只是在 Docker 内部运行 vsdbg 并附加到这里.但显然这是一个非常常见的开发用例,奇怪的是没有这方面的好信息.

I guess there shouldn't be much specific of Docker here, it's just running vsdbg inside Docker and attaching here. But obviously it's a very common dev use case and it's weird that there's no good information on this.

当然,有 VS Tools for Docker 使用它我们可以轻松地对 Docker 容器内的应用程序进行调试.但对我来说,VS Tools for Docker 简直太糟糕了.是的,它们一开始可以无缝地工作.但目前还不清楚到底发生了什么.

Surely, there are VS Tools for Docker using which we can easily do debugging of an application inside a Docker container. But for me, VS Tools for Docker are just terrible. Yes, they work seamlessly at first. But it is absolute unclear what is going on under the hood.

看来我们只能查找 VS Tools for Docker 做了什么并尝试重现它.但这不是很明显.它增加了一个额外的调试"YAML 文件到 docker-compose (docker-compose.vs.debug.g.yml) 应该执行调试魔法.我尝试将该 YAML 内容添加到我手写的 docker-compose 中,运行 Docker,但如何附加 Visual Studio?

It seems that we just can look up what VS Tools for Docker do and try to reproduce that. But it's not very obvious. It adds an additional "debug" YAML file to docker-compose (docker-compose.vs.debug.g.yml) which should do the debugging magic. I tried to add that YAML content to my hand-written docker-compose, run Dockers, but how can I attach Visual Studio?

我获得了容器的 IP 地址,试图在该 IP 地址上找到远程调试器,结果是 4022,Visual Studio 看不到任何内容.另外值得怀疑的是,由 Tools for Docker 创建的文件 debug.yaml 与预期的 4022 端口无关.

I get the IP address of my container, tried to find a remote debugger on that IP address and 4022 that Visual Studio can't see anything. Also it's suspicious that file debug.yaml created by Tools for Docker has nothing about exposing the 4022 port as it could be expected.

PS:我发现一个很好的指南,但在 Windows 容器上

推荐答案

这个怎么样:

如果您的服务基于 microsoft/dotnet Docker 映像,请基于相同的映像创建一个新的 dockerfile,然后安装调试器、ssh 和解压缩.

If your service is based off of the microsoft/dotnet Docker image, create a new dockerfile based on the same image, and install the debugger, ssh and unzip.

FROM microsoft/dotnet

RUN apt-get update && apt-get -y install openssh-server unzip

RUN mkdir /var/run/sshd && chmod 0755 /var/run/sshd
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin without-password/g' /etc/ssh/sshd_config
RUN sed -i 's/#StrictModes yes/StrictModes no/g' /etc/ssh/sshd_config

RUN service ssh restart

RUN mkdir /root/.vs-debugger && chmod 0755 /root/.vs-debugger
RUN curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v vs2017u1 -l /root/.vs-debugger/

EXPOSE 22

构建并将其推送到您的注册表.

Build and push this to your registry.

docker build -t myregistry/dotnetdebugger .
docker push myregistry/dotnetdebugger

接下来确保您的服务构建将 PDB 文件输出为可移植 PDB 文件 -从 Visual Studio 在 Linux 和 OS X 上对 .NET Core 进行越野调试

Next ensure that your service's build is outputting the PDB files as portable PDB files - Offroad Debugging of .NET Core on Linux and OS X from Visual Studio

并确保在构建服务的 Docker 映像时 PDB 文件包含在 DLL 文件中.

And ensure that the PDB files are included with the DLL files when you build your service's Docker image.

然后当您的容器正在运行并且您决定需要对其进行调试时,您可以将调试器容器作为 side car 容器附加到服务中:

Then when your container is running and you decide that you need to debug it, you can attach the debugger container as a side car container to the service:

docker run -d -p 10222:22 --pid container:<container name> - myregistry/dotnetdebugger

然后在 Visual Studio 中,转到菜单 ToolsOptionsCrossplatformConnection Manager - 并添加一个新的连接.指定容器的 IP 地址或主机名,10222 作为端口(docker run 命令中的那个),root 作为没有密码的用户.

Then in Visual Studio, go to menu ToolsOptionsCrossplatformConnection Manager - and add a new connection. Specify the IP address or hostname of the container, 10222 as the port (the one in the docker run command), and root as the user with no password.

这篇关于如何从 Visual Studio 调试在 Linux Docker 容器中运行的 .NET Core 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆