How to debug a .NET Core app running in Linux Docker container from Visual Studio

爱⌒轻易说出口 提交于 2019-12-03 12:05:38

How about this:

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 

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

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 

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.

Hope that helps

As of today, if you are using visual studio, you can use their official support.

You just need to have installed docker and add the support for docker projects Project->Docker support.

This creates a new project with a docker compose and a dockerfile to your project, then VS links this and allows debbuging!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!