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

I have my own hand-written Dockerfile / docker-compose files. I run containers from the command line. Now I want to connect VS2017 (not VSCode) to my application in a Docker container (based on Linux). This seems to be a pretty easy task, but I cannot find information on how to do this.

I carefully read the manual https://github.com/Microsoft/MIEngine/wiki/Offroad-Debugging-of-.NET-Core-on-Linux---OSX-from-Visual-Studio . At first it looked the way I needed - a description of the remote debugging of a netcore application running on Linux. But he only tells part of the story - how to debug through SSH. And it just mentions Docker, but says nothing about how to remotely debug an application inside Docker.
I believe that there should not be a special Docker here, it just launches vsdbg inside Docker and joins here. But obviously, this is a very common use case for dev, and it is strange that there is no good information about this.

Of course, there are VS Tools for Docker, with which we can easily debug the application inside the Docker container. But for me, VS Tools for Docker is just awful. Yes, at first they work without problems. But it is completely unclear what is happening under the hood.

It seems that we just can see what VSTools does for Docker and try to reproduce it. But this is 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 added add this yaml to my handwritten docker-compose, run Dockers, but how to connect VS? I get the IP of my container, trying to find a remote debugger by this IP and 4022, which VS does not see anything. It is also suspicious that the debug.yaml created by Tools for Docker has nothing to do with setting port 4022, as you might expect.

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

+7
2

:

microsoft/dotnet, docker , ssh 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  

.

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

, PDB dll Docker .

, , , :

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

Visual Studio > > > - . IP , 10222 (, docker run), root .

,

+3

, Visual Studio, .

Project->Docker support Project->Docker support.

docker compose dockerfile , VS !

0

Source: https://habr.com/ru/post/1693313/


All Articles