Debug docker container | live debug docker image using VsCode | #docker #devops

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
building packaging and running containers is fun but then it's not fun when you hit an exception and you're like wouldn't it be cool if i could just debug this code from visual studio code attach it to a container so i can really see the line where the code is breaking well guess what with the docker extension in visual studio code it's absolutely possible to do that so in this video we're going to work from start to end by creating an issue building the container identifying the exception then running through the steps of how to configure visual studio code to run in debug mode against a container fix the issue and then re-run it with the issue addressed so stay tuned in the next 10 minutes we'll go from nothing to everything and then see how it all works together [Music] so let's start off by checking the list of all the git branches that we have for the current repo okay let's uh create a new branch and we can do that by checkout and minus b to check it out and create a new branch let's call it docker live debug docker now this branch is where we will run the rest of the video from let's open a visual studio code as you can see in the visual studio code i've got in my web project and then i've got a my web test project and then i've got a docker file look at the link popping up in youtube that explains uh so far how to create the mywebproject.net core vanilla as well as the basic docker file now you can see within this directory i've got a docker file and i can run cat docker file just to see the contents of it now let's start off by actually building uh from this docker file and we can do that by running the docker build and by saying dot we're saying from the current context and then using the minus t to specify the tag that needs to be used with it so let's just uh do that and then minus f specifying the file that needs to be used so in this case we're using the docker file that i just showed you now when we when we run this command it just works through the docker file to basically execute the steps that are listed there and in this case it's just basically fetching one of the existing ms docker images and then bolting on our app on top of it now once the build is complete you can see a new image has been created and we call the image my web now we're simply going to run the uh image in a detached mode attaching the process of port 80 to 8080 on my local machine and let's just run it giving it a name my web and then we're saying when you run my web use the my web colon debug image which we previously produced now because it's running in detached mode we'll have to open the browser and as we open the browser we can browse to port 8080 on localhost and we can see the container now i can click on home it's good but when i click on privacy or privacy as some of my audience would prefer it to be called we see we've run into an exception now for one i could navigate to docker logs and within the docker logs i could look at my web uh logs to figure out um you know the stack trace of the failure but it's not the most intuitive way to work through issues this is still a very basic project but what if you had a quite a complex big project to sort of troubleshoot now in this case i could go into visual studio code i can try and look at the code and the exception in the logs and try and work out where the issue is again in this case it's it's fairly straightforward you know i embedded the issue so i very well know what you would have also seen on the screen is i'm using the debugger command there to uh sort of pause in debug mode but it's not doing that which is clearly telling me that the uh that my session is not running in debug mode now let's look at this docker extension because this docker extension in addition to all of the other cool stuff it provides also gives you a way of executing a lot of the docker commands right from vision within visual studio code as you can see on your screen when i type docker in the help menu i should be able to see all of the commands and then start to choose from those commands what i want to run let's just try that by typing docker and you can see we we get a view of all of these commands which is which is quite handy dandy now in this case the first thing we need to do after we've installed this extension is to add build artifacts for the dotnet project that we have and we can do that by using the.net generate builds and debug artifacts and you see it adds the launch.json and the task.json now task.json is the one that's of interest to us let me just collapse the whole thing and you can see it adds a section for builds where it just basically uses an entry point of my web um and then that's in essence the configuration that's needed to run the project in build mode and you can see the moment i do that now in the debug section i have the option of selecting one of those tasks to run my project now the next thing i need to do is i need to run the docker command for initialize for docker debugging and because this is an asp.net core project that's the type i want to use and my base image is running linux so i have selected that as well now let's close it and open it again and really what we would see once we do that is uh there is now an addition of docker build um um configuration type let me just correct the path because it's added in my web folder in front of my docker file and as i showed you the my docker file is at the root so let's just quickly clean that out now in essence if i go into the debug mode now i should see one more configuration in there the third one the docker.net core launch so let's close this and clear this out just uh we'll quickly show you that in if i run docker ps i see that there are no currently running docker containers i just wanted to show you that that i don't have anything running already but if i select the debug profile for docker now and run it it's essentially going to go through my docker file rebuild the container from the image create an image on the back of that and then use one of the added tasks to take that image and run it and then opens up a debug view for me quite nicely let's just add a few debug points in here so that we can when click clicking on the web be able to have them relay back into visual studio code now as you can see it's just directly hit the debugger and i'm able to browse my home controller code which was always working fine so nothing to see here let's click on privacy we're going to navigate the same code again but as we work through this we're going to come to the privacy function where we will see the exception happening and this is me seeding this code of you know a a function that's not yet implemented it's always going to throw an exception as i said right simple if you know where it is and not very simple when you have to work through the code and the logs to sort of figure that out and you get a lot of nice help here right you get the capture of all the headers the routing the full debug experience as you would expected so the the fix here is fairly easy let me just comment this bit of code out well actually let me just comment out um the whole function which i created so that we could just uh fix the issue let's just remove the message from the return view as well and see i'm not debugging or manually building here i'm simply going to go back to my profile and hit um hit f5 again in essence that's going to use the profile to run the entire container from build create the image and then run the container image and come into the context of the debug let's just hit f5 and now if i go into privacy and work through skip there you go the issue is fixed right [Music] so hopefully you've seen how easy it is to use the out of box uh extension uh for docker available in vs code to apply and bring the configuration that's necessary to run a container uh in debug mode and have the full debug experience as if you were developing the app natively now this is one of the many videos i've created in docker uh be sure to check out the the channel and the full playlist for docker where we start off by creating a full docker file from nothing so understanding what the from command is used for what's the benefit of using multi-staging within a docker file and then creating an azure yaml pipeline the azure devops yaml pipelines that's right to natively build a container generate an image and through those videos we also work through how to run unit tests within a container by layering on on top of the container image a temporary image in order to run the test and then publish the test results from within azure pipelines so that you can visualize any failing test and stay tuned this isn't the last of the videos because and up next i'm gonna do a video that covers how to collaborate when you're working in visual studio code and especially when you're doing something complex like a container and have that social collaborative uh environment right available from visual studio code to brit to bring in uh and simplify team collaboration so that's it for me for now and thank you for watching don't forget to hit the like button and subscribe if you are enjoying the videos and i would much love if you can tweet this uh this about this video as well i've been greedy of course but then the contents free right so if you enjoy it be sure to give back thank you you
Info
Channel: Azure DevOps TV
Views: 9,914
Rating: undefined out of 5
Keywords: docker tutorial, docker, docker container, docker container for beginners, azure pipelines yaml, azure pipelines, azure pipeline docker build and push, kubernetes cluster, azure container registry, ci/cd pipelines, devops, docker hub, azure devops, azure devops tv, azure devops tutorial for beginners, dockerfile tutorial, multistep docker file, dockerfile multistage, dockerfile multiple containers, docker beginner tutorial, docker beginner, cicdpipelines, docker debugging
Id: k0hpant9wXo
Channel Id: undefined
Length: 11min 31sec (691 seconds)
Published: Sun Sep 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.