问题
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.
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.
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.
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. found a good guide but on Windows containers - https://github.com/riskfirst/debugging-aspnet-core-windows-docker
回答1:
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
回答2:
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!
来源:https://stackoverflow.com/questions/48661857/how-to-debug-a-net-core-app-running-in-linux-docker-container-from-visual-studi