Debugging .net core application running in docker container, from visual studio 2017, in a remote linux machine.
Off late i have been doing a lot of exploration of docker technology, though a lot has been written about docker technology but about one topic on which i could not found much detail was Debugging in docker containers.
Following are the tools which i am using for exploration-
I have created a docker image of this application and pushed to my private registry so that i can access it from my ubuntu machine.
my docker file look like below-
Please note that we need to expose port 22 from container so that we can ssh in to it.
I have started a docker container in my ubuntu machine using below command -
i am exposing container port 22 via host port 10222.
now in order to debug the application running in this container from windows machine containing visual studio broadly below steps needs to be done-
once in set proxy if you are behind corporate proxy using
Following are the tools which i am using for exploration-
- Visual studio 2017 version 15.3.4 with .net core 2.0 sdk on windows 10 machine
- Docker for windows.
- Ubuntu 14.04 machine with docker package installed.
Visual studio 2017 magic -
Visual studio has done a great work to add support for docker in your project. its just few simple
clicks which perform all the work and you will not even realize that you are debugging a docker container or normal exe, more details can be found here.
above experience is good for local development but what if your docker container is running in a production linux machine with no visual studio installed in that , how do you debug using visual studio in that scenario.
Debugging in Remote Linux Machine using Visual studio 2017 -
To showcase how to do same , i am using a simple .net core application having below code -
I have created a docker image of this application and pushed to my private registry so that i can access it from my ubuntu machine.
my docker file look like below-
# Use the standard Microsoft .NET Core container
FROM microsoft/dotnet
COPY PublishOutput/ .
EXPOSE 22
ENTRYPOINT ["dotnet","TestOnceAgain.dll"]
Please note that we need to expose port 22 from container so that we can ssh in to it.
I have started a docker container in my ubuntu machine using below command -
docker run -d -p 10222:22 RegistryIP:port/imagename
i am exposing container port 22 via host port 10222.
now in order to debug the application running in this container from windows machine containing visual studio broadly below steps needs to be done-
- install ssh in container.
- allow root to ssh in this container
- install vsdbg in container
- perform connection setting in visual studio to ssh in to ubuntu machine.
lets look at it one by one.
1. Install ssh in container
run the following command to enter your docker container -
docker exec -it /bin/bash
once in set proxy if you are behind corporate proxy using
export http_proxy=http://IP:PORT
export https_proxy=http://IP:PORT
and then below commands to install openssh server and start it.
apt-get update
apt-get install openssh-server
mkdir /var/run/sshd
chmod 0755 /var/run/sshd
/usr/sbin/sshd
2. Allow root to ssh in this container
once ssh server is running we generally have to create a user which will be allowed to ssh in the container , but creating a new user create a problem during debugging from visual studio since our original application runs under root account , so when we try to debug from a different account(one created specifically for ssh) we get this wrongly titled exception from visual studio.
Hence instead of creating new user we should allow ssh from root which can be done using below steps -
apt-get install vim -- install vim
vim /etc/ssh/sshd_config
in config look for
PermitRootLogin without-password
change it to
PermitRootLogin yes
sudo service ssh restart
3. Install vsdbg in container
Visual studio uses VSDBG to perform remote debugging in container in order to do so it needs VSDBG in container, along with unzip package to unzip VSDBG, install them using following command, -apt-get install unzip
/root/.vs-debugger/GetVsDbg.sh -v vs2017u1 -l "/root/.vs-debugger/vs2017u1"
this completes all the steps which we need to do in container now lets head to visual studio .
4. Perform connection setting in visual studio to ssh in to ubuntu machine.
In VS 2017 installed on windows machine go to Tools -> Options -> Crossplatform -> Connection manager
click on add and make entries
once done pres ctrl + alt + p ( attach to process) and set connection type as SSH and connection target as your configured connection and select dotnet there -
click on attach and tick on managed.
and done you can debug your application -
This comment has been removed by a blog administrator.
ReplyDelete