Debugging a Dockerized Django app with VSCode

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in our advanced course on building rest apis we teach how to build a django rest framework project using docker we like to use docker and docker compose when working with django on our local machine for many reasons one is that it helps to isolate the dependencies on our machine from the dependencies of our project which reduces conflicts between different projects on our machine another is that it makes for a consistent development environment for all developers that work on the project this can save a lot of time when developers are rolling off and on a project because they don't need to spend a lot of time configuring their local machine to run the project they can simply just have docker installed and run the project inside docker it also helps you to have parity between your development environment and your deployment environment this means that the image that you are running on your local machine is very similar to the one that you're running in production this helps get rid of all the issues such as it works on my machine but it doesn't work in production however this process doesn't come without drawbacks one of the issues that students ask about all the time in our course is how do you use the integrated debugging tools with a visual studio code when you're working on a project using docker this can be difficult because the python interpreter that is running for our project is going to be inside the docker container and our local machine doesn't have direct access to this container therefore we need to make some changes on our project in order to integrate visual studio code with docker so we can debug our project so in this tutorial i'm going to show you how to do just that we're going to start by creating a new django project and then we're going to configure it using visual studio code to debug via docker and then i'm going to walk you through how you use some of the basic debugging tools that comes built in with visual studio code so let's get started we're going to start by installing some dependencies on our machine i'm not going to walk you through how to install this because they should be pretty self-explanatory but the list of dependencies that we're going to need starts with docker desktop so if you're using windows or mac you want to make sure you have docker desktop downloaded and installed on your machine if you're using linux then you're going to need to install the docker daemon separately and also docker compose and make sure you have both those tools updated and working on your linux machine secondly we're going to need the visual studio code editor so you can get this again i've linked all of these in the description of the video download this and make sure you have it installed and set up on your machine if you don't have it already then we're going to need two extensions that come with visual studio code the first one is docker so make sure you have the docker extension installed the one that is produced by microsoft and the second one is python you want to have the python extension installed and again you want the official one by microsoft once you have these dependencies installed we're ready to get started we're going to start by creating a new directory on our computer that we're going to use for this project so i'm going to open up the terminal or if you are on windows you can use the command prompt window i'm going to navigate to the location i like to store my projects so that is in document workspace and then i'm going to create a new directory for our project and i'm going to do this by typing mkdir vs code hyphen django hyphen docker hit enter then i'm going to move into that directory and then i'm going to initialize it as a git project by typing git init once that's done we can go ahead and open this project in visual studio code i'm going to do that by typing code dot if you prefer you can open up visual studio code and you can open up the project directly using the interface once you have the project open we're going to add a couple of basic files to the project just to get started we'll start by adding a readme i'll do this by typing readme.md and i'll add vs code docker django debugging then i'm going to add a git ignore file for a python project so if you go ahead and create a new file in the root of the project and call it dot git ignore and this is going to contain a list of files that we want to exclude from our get source control these are things such as binary files and things that are generated in real time by python that we don't necessarily want committed to our project source control i like to use the template python git ignore file that is provided by github that will also link to in the description of this video so let's go ahead and copy the contents of this and then i'm going to open up visual studio code and just paste it in i'm also going to add ds underscore store which is an annoying file that is generated automatically by the finder on mac machines now that we've got that set up we can move on to actually creating a django project within our directory i like to put the django project in a directory in our project called app so i'm going to create a new folder here called app and the reason i like to do this is because it helps separate our django app code from the rest of the code in the project so if you have any given django project then you may have various different code items that are not specifically related to the django code but are things like the continuous integration or the dockerfile for the project and things like that things that aren't actually needed for the django python code but just for the project generally so i like to put them in the root of the project and everything django related in an app directory now that we have our app directory let's go ahead and create our django project i like to do this using docker that way i don't need to have django installed on my local machine i don't need to worry about what version of django is installed on my local machine or the machines of other developers who are working on this project i'm going to create the project by using a one liner that i created which runs a docker container installs django and then creates the project so it's quite a long command but you should only need to type it once i will provide a link in the description of a gist to the command if you just want to copy and paste it go ahead and open up the terminal or the command prompt window and i'm going to do this by typing docker run hyphen v and then this dollar sign open curly braces pwd in caps close the curly braces colon no not colon forward slash app colon forward slash app hyphen w forward slash app and then python colon 3.9 hyphen alpine sh hyphen c and then in quotations pip install django equal equal 3.2 two and signs and then django hyphen admin start project app and then a dot at the end and then we're going to close the quotations here i'll just quickly walk through what this does because you're probably curious about what all of this means so we're using the docker run command this is the command that comes when you install docker that allows you to run a docker image as a container on your machine then we specify hyphen v and then this bit here so this will map a volume from our local machine to the app volume on our docker container what this does is it allows the docker container to have access to our app directory in our project this is important because when we create the new project in the docker container we want the outcome of that project so the source code files that are generated to be available in our code project on our local machine and this is how that happens here so the dollar sign and then this pwd here is print working directory that prints the current directory that we're in and then it's forward slash app because we created a sub directory called app and then colon says we want to map this part on our local machine to this part on our docker container and we're mapping it to just forward slash app in our docker container the next part of the command is this hyphen w forward slash app and what this does is it sets the working directory for our docker container to forward slash app and the reason we do this is so that when we create the new django project it is automatically created at this location specifying this is similar to specifying the cd command before you run the actual command for creating a django project next we have this python colon 3.9 hyphen alpine what this will do is it will download the python alpine image from the docker hub which is a publicly available image and then it will use it to run our container finally we have this sh hyphen c and then the commands that we're running inside the container so sh hyphen c tells docker that we want to run a shell command when we start the container and the shell command that we're passing in is what is inside these quotation marks here so first we're doing pip install django with the version pin to 3.2 this will download and install django 3.2 in the container once it started then we have these two and signs which basically says we want to run another command after we do that and then we specify the last part of the command django hyphen admin start project app and then the dot because we want to create the project in the current directory and not create another sub directory inside that project so that's an overview of the command once you've typed that all out you can hit enter and this will go ahead and download the image from docker hub and it will run the command and if i just move this out of the way here and expand this app directory we should see the files appear there once this process is complete now if you have the alpine image already cached on your machine then it will skip this step because it doesn't need to pull it down again from the docker hub it will already be cached because i don't have it cached it had to download it and run it [Music] now let's run the command you can see in our project we have the app and the manage.py files these are auto generated by the django admin style project command now that we've done that we can move on to the next step which is to create the template docker configuration files in visual studio code fortunately we don't need to create all these files ourselves from scratch we can have visual studio code auto generate them and then we can modify them to fit our project so we can do that by opening up the palette so the command palette with visual studio code is accessible on a mac os by pressing command shift p or on windows it will be control shift p then you can type the type of command that you want to search for so we want to type add docker and you should see this one here add docker files to workspace now if you hit enter there to select it it will go ahead and ask you what type of application platform you want to create we're going to choose python django and then it will ask you where the app's entry point is located so our entry point is actually located here in app forward slash manage which is inside our directory here it was auto generated by django this is the entry point to start the debugging for our application so we can select that one here at the top app forward slash manage.py now it's asking what port we want to listen to we can leave it at port 8000 hit enter now it's going to ask us if we want to create a docker compose file so we're going to choose yes so we have the docker compose files created as well and then it will go ahead and auto create all of these files for us if you're on a mac or linux machine you might notice that the line endings are automatically set to clrf this can be a bit annoying when you're committing these projects to get so we're going to go ahead and change them what we need to do is click on the clrf here change it to lf and then choose the other files that were generated so that is the docker compose file do the same there and then the other docker compose file change that to lf and then i think that is everything yes so that's all changed and you don't need to do this if you're running on windows because the crlf line endings are okay for using windows now these template files are a great start for working on a project however they might not be exactly what you want for your project the chances are that you might need to modify some of these files in order to get them to work for your project and that's exactly what we're going to do now let's start by opening up the requirements.txt file and again if you're on a mac or linux make sure that's changed to lf and we're going to change these dependencies here so what i'd like to do is specify django greater than equals 3.2 comma less than equals 3.3 this will make sure that every time we build our docket images any of the latest patch releases that contain security fixes and things like that like django 3.2.1 should be installed automatically i'm going to do the same for the g unicorn requirement here so i'm going to do more than equals 20.0.4 and less than 20.1 again so we can capture any of the patch updates every time we build out containers you can remove this comment here from the top if you like now let's go ahead and save the requirements file now we can move on to the next file next we're going to modify our docker file let's go ahead and open up the docker file here and we're going to make a few changes to this file the first change is i'm going to switch it to use the alpine image i like to use the alpine image because it's more lightweight than this slim buster image and i prefer to use it when i'm working on projects wherever possible so let's change this file that says from python 3.8 we're going to delete this bit here we're going to change it to 3.9 because we might as well use the latest version at the time record in the video then we'll do hyphen alpine 3.13 so then we're going to be using the python version 3.9 with the alpine linux version 3.13 and i'd like to pin the versions to as specific as possible so that if there are any updates i can manually upgrade them in the project and not have them automatically break my build next we're going to change the copy line here and i'm just going to change it to dot forward slash app and what this does is it copies the location of our app inside our project to the forward slash app location in the docket image that is built and we do this because we don't need all of these files here included inside our docker file we just need the app that contains our django application finally we just need to make one last change to the bottom here and that is to replace this pythonpath.2.wsgi and we just need to change it to app.wsgi the reason we need to do this is because this is the location of our wizki file in our project is the one auto generated by django so if you remember that we are in the workdir app because we set worked app here so the location when we start the g unicorn service is going to be in app.w so that maps to this app.wsgi here now that we've made this change we can go ahead and save the file and then let's open up our terminal or the command prompt window and we're going to type docker hyphen compose build and this will build our docker image ensuring that our new docker file is all correct so this should build successfully we just need to wait for the image build process to complete and then we'll continue [Music] once the build command is finished we can type docker hyphen compose up and this should start our service so we should be able to access our service once this starts and test out our django placeholder page okay so the service is started let's open up our browser and let's just head over to 127.0 colon8000 hit enter and you should see this django 3.2 placeholder landing page so this means that we've configured our docker files successfully in order for debugging to work through visual studio code there's a couple more changes we need to make the next change we're going to make is going to be to a docker compose file so first let's go over to terminal and just press control c to stop this server from running now let's open up visual studio code again and i'm going to choose the docker compose.debug.yml file so this is the yaml file that configures dockercompose for our debugging we just need to make a small change to this file and that is to scroll over a bit here and locate the app forward slash manage.py we're going to trim this app forward slash off the beginning so we'll just remove that then save the file and that's the only thing we need to change in this particular file next we need to change the launch file so if we look in our project here at the top we've got vs code and then we've got a launch.json file so this is the configuration that launches our project and again we just need to make a small change here the change we're going to make is to add a forward slash app to the end of this workspace folder so that it maps the correct directory for our project to the correct place in our container so save that file and now open up this tasks.json file and we need to make a similar change here but we just need to change this app here and delete the contents of it so it just says manage.py and then save the file and the reason we do this is because when we're running our container we're going to be working from the app directory that already has our manage.py file in it so we don't need to specify the path app forwards that's manage.py because when we're inside docker we're going to be actually working from the directory so we can just call manage.py directly that's all the changes we need to make in order to configure our project for debugging now let's go ahead and test the debugger tools and actually debug some code so if we go ahead and close these files out and then what we're going to do is we are going to add a new view to our project so inside app forward slash app i'm going to create a new file called views.py and we're just going to create a sample django view that we can use for testing so i'm going to start by doing from django.http import http response and then def index and accept the request as an argument and then we'll do return http response and i'll just add some sample response code here so just say hello world you can put whatever string you want here now save the file and now we just need to wire this view up in our urls.py so we'll open up urls.py and then underneath this path admin here we're going to add a new line that says path and we'll just add an empty string comma and then index and then at the top here we're going to import the index so we're going to do from app.views import index this will import the index view that we just created in our views module here now you can save the file and now we can actually test the debugger so let's go ahead and place what's called a breakpoint in our code so we go to views.py and if you move the mouse next to the line number you should see this little red dot appear this allows us to place a breakpoint in our code if you click that you should see the red dot gets lighter and stays a breakpoint is basically used to pause the execution of our code on a particular line and it allows us to be able to perform some debugging tasks on that line for example let's say our http response wasn't returning what we expected we can place a breakpoint here and we can run the debugger and then we can inspect the variables and see what's in the different variables and the current state when the code gets to this line so it's a really useful tool for debugging let's go ahead and test it now by clicking on run start debugging this should go ahead and start our project in the debugger mode and we should be able to see what happens on this line here so you can see that the code execution actually paused here you can see when it turns orange like this that the code has stopped running our project will no longer be actually running it's paused for us to do some investigation here you can use the debugger mode so if it didn't automatically switch you to this screen then you can click on this bug icon here and it will take you to the screen here and here you can see all the different variables so i mentioned the request variable that is the request object that is passed to the view when we make a request to django here so if we wanted to inspect the details of that we can do this here in the browser on the left hand side so you can click the arrows here and you can see all of the contents here and this just gives you more information to use when you're trying to figure out what is wrong with your application if you want to continue the execution of your code then you can click on this play icon here or the continue button that will continue the execution and the application should be loaded up in our browser you can see that because there were no more breakpoints in our code it continued to the end and actually rendered the page so this is actually our application running now if you go back to vs code you can stop the debugger by clicking on the stop button here so that's how you set up visual studio code to be able to use the integrated debugging tools on a django application that is running in docker i hope you found this video useful if you have any comments or feedback or there's another approach that you prefer to use when you're debugging your django applications with docker and please leave them in the comments below because they always generate an interesting discussion and we can always learn something from each other i hope you found this useful and thank you so much for watching
Info
Channel: London App Developer
Views: 35,042
Rating: undefined out of 5
Keywords: django, docker, vscode, debugging, ide, integrated debugger, python
Id: x7lZAmMVo2M
Channel Id: undefined
Length: 23min 22sec (1402 seconds)
Published: Mon Apr 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.