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

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

问题描述

我自己手写了Dockerfile / docker-compose文件。我从命令行启动容器。现在我想将VS2017(不是VSCode)附加到我的应用程序中的Docker(基于Linux)容器中。看来这应该是一件容易的事,但我找不到有关如何执行此操作的任何信息。

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

我通读了指南 https://github.com/Microsoft /MIEngine/wiki/.NET核心在Linux上的Offroad-Debugging --- Visual Studio的OSX 。最初,它看起来像我所需要的-有关如何远程调试在Linux中运行的netcore应用程序的描述。但这仅说明了故事的一部分-如何通过SSH进行调试。只是提到了Docker,但没有提到如何在Docker中远程调试应用程序。

我想这里对Docker的要求不高,它只是在Docker中运行vsdbg并附加在这里。但是很明显,这是一个非常常见的开发用例,很奇怪,对此没有很好的信息。

I read through the guide https://github.com/Microsoft/MIEngine/wiki/Offroad-Debugging-of-.NET-Core-on-Linux---OSX-from-Visual-Studio carefully. At first it looked like what I needed - a description on how to remotely debug a netcore app running in Linux. But it only tells a part of the story - how to debug via SSH. And just mentions Docker but says nothing to how to remotely debug an app inside Docker.
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 Docker工具,通过这些工具,我们可以轻松地在Docker容器内调试应用程序。但是对我而言,VS Tools for Docker简直太糟糕了。是的,他们一开始无缝地工作。但是绝对不清楚幕后到底发生了什么。

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

似乎我们只是可以查找Docker的VSTools所做的事情,然后尝试重现该内容。但这不是很明显。它将一个额外的调试 yaml文件添加到docker-compose( docker-compose.vs.debug.g.yml ),该文件应具有调试作用。我将那个Yaml添加到我的手写docker-compose中,运行Dockers,但如何附加VS?我获得了容器的IP,试图在该IP和4022上找到VS无法看到的远程调试器。同样令人怀疑的是,由Tools for Docker创建的debug.yaml与公开4022端口没有任何关系。

It seems that we just can look up what VSTools 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 tied add that yaml to my hand-written docker-compose, run Dockers but how to attach VS? I get IP of my container, tried to find a remote debugger on that IP and 4022 that VS can't see anything. Also it's suspicious that debug.yaml created by Tools for Docker has nothing about exposing 4022 port as it could be expected.

P.S。在Windows容器上找到了很好的指南- https://github.com/riskfirst/ debugging-aspnet-core-windows-docker

P.S. found a good guide but on Windows containers - https://github.com/riskfirst/debugging-aspnet-core-windows-docker

推荐答案

如何操作:

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

If your service is based off of the microsoft/dotnet 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
https://github.com/Microsoft/MIEngine/wiki/Offroad-Debugging-of-.NET-Core-on-Linux---OSX-from-Visual-Studio

Next ensure that your service's build is outputting the PDB files as portable PDBs https://github.com/Microsoft/MIEngine/wiki/Offroad-Debugging-of-.NET-Core-on-Linux---OSX-from-Visual-Studio

并在构建服务的docker映像时确保PDB文件包含在dll中。

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

然后,当容器运行时,如果您决定需要对其进行调试,则可以将调试器容器作为侧面车载容器连接到服务:

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中,转到工具>选项>跨平台>连接管理器-并添加一个新的连接。指定容器的IP或主机名,将10222作为端口(docker run命令中的端口),将root用户作为没有密码的用户。

Then in visual studio, go to Tools > Options > Crossplatform > Connection Manager - and add a new connection. specify the IP 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天全站免登陆