Debugging .NET Core in Docker with VSCode

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is our people and today we're going to be looking at debugging dotnet core code inside of a docker container let's go so the first thing I want to show you guys is how to use docker multi-staged to basically slide in a debugger in your debug layer of your image so you can expose a port attach a debugger and you can use the same docker file for development and production so you don't need to separate docker files I'm gonna show you all the mechanics and we can achieve this using visual studio code so you don't need full-blown Visual Studio Enterprise so let's check it out so just before we continue I want to show you guys the link in the description of this video will lead you to the docker development youtube series github repo or the code I'm showing you is part of this github repository and I have a playlist on YouTube and every part is there's a URL for every part here as well as the source code so if you click the source code for example we go to part 1 all the code here will be related to part 1 you can go to part 2 3 & 4 you can watch the video as well as follow along with the source code so firstly let's take a look at the docker compose file we have a c-sharp service we give it a container name called c-sharp I give it an image name as well and then here I passed the build context this is important the c-sharp folder points to my local c-sharp folder over here and you can see my source code is inside the source folder we also make a target called debug and I will show you guys about docker multistage but we are interested in creating a debug layer here then we create a volume where we say c-sharp source mounted into our working directory that's basically mounting the source code here inside the container so we don't need dotnet installed on our host and then I'm also exposing a port for my application [Music] now let's take a look at our docker file for the c-sharp application you can find it under the C sharp folder docker file and in here we say from and we call the da min SDK and we mark that as debug this is a multi-stage feature this means that everything from this from statement all the way down to the next from statement just before that that becomes the debug docker image so you don't need to docker files you only have one debug layer which is the debug layer and then we have the next layer which is the prod layer and you can see that we use the runtime in the production image and not the SDK that's how we keep our production image small and we also going to use a debugger and our debug layer so this prevents us from pushing any debug libraries and stuff into production so let me quickly run through this we have the SDK we create a work directory we copy our source code into the work directory but firstly the project file then we do dotnet restore because we don't want dotnet restore to run every time we change source code so we only copy in the project file first Rand net restore then we copy the rest of the source code in and we do dot net publish and this gives us a release build and then what we do here's the magic we do apt-get update we install unzip we're gonna need unzip is a dependency and then we install the microsoft visual studio debugger with this line over here that's gonna install the debugger inside of the container inside the debug layer image and then we do dotnet run now if we go back to our docker compose file by specifying a target as debug here that means we want to work locally we only want to build up to the debug layer obviously if you want to test the production image you would change this over to product the next part you need to need is your gonna have have to hit over two extensions and what you want to do is install the.net extension for the c-sharp extension so you want to go ahead and search for c-sharp and you will find the Microsoft one that I already have installed here go ahead and install that and then restart your Visual Studio code just in case and then what you want to do is go over to debug and you will see nothing in this drop down if you have no debugger installed yet so what you wanted to do is click this gear icon that'll generate a new launch JSON file the launch dot JSON file is found right there it'll create a dot vs code folder with a launch to Jason you can find this file in my git repository so you don't have to go and find the stuff to paste in here because Visual Studio code will just generate a blank one for you you need to fill it out so in my previous example my previous video we did golang in a container so this is two debugger application you're going to need this part here and this is - this is the docker attached debugger for dotnet so we're gonna say core CLR as a type and it's an attach request so the application will run and when we click debug we'll attach to it you can see here we using docker under the hood docker exec to basically get an SSH into the container and the most important parts are these two sections here this one is - way to find the debugger and this one is way to find your source code and then we want to map our source code into the container so we basically here we say slash work this is where we find the code inside of the container and this is where we find the code outside of the container so this is how the debugger knows the way to put the breakpoints outside of the container and we're to find the actual running code in the container so you don't need dotnet installed on your host to get this done okay so what I'm going to do is I'm going to say docker compose up c-sharp and that'll start up our container and the entry point for that one I think is just dotnet run so we can see here we run it's running and it's listening on port 5000 so it's basically up and running ready for us to attach a debugger so the next thing we're going to do is we're gonna head over to our debugger here and we can to hit the play button and then what we want to do is select the write process because the remembered dotnet has a couple of child processes and this will be the one this is our dll that we building so I'm going to select it and the debugger is loading up and we're in business alright so I have my application up you can see there's a privacy link that we can click and what I'm gonna do is I'm gonna go find that source code so if we look on the left hand side here we should see privacy dots CS HTML dot CS and let's just put a breakpoint over there and let's click that button and boom here we go there's our breakpoint is hit and we can go onto the debugger and we can start seeing all the variables and stuff so breakpoints work and that's how you do a container so I hope you guys found it useful I get a lot of questions from people when it comes to containers how to debug code inside of a container and etc and there's literally no magic you just need an extension and the extension as I showed you has a mapping to say where the source code is locally and where to find it in the container and some debuggers listen on a port other debuggers use SSH other ones will just use the docker docker exec which is like SSH the container so hope you guys enjoyed it let me know down in the comments what other videos you'd like me to do and yeah like and subscribe and peace [Music]
Info
Channel: That DevOps Guy
Views: 14,973
Rating: undefined out of 5
Keywords: vscode, dotnet, csharp, docker, coding, debugging
Id: ds2bud0ZYTY
Channel Id: undefined
Length: 8min 14sec (494 seconds)
Published: Sun Aug 04 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.