Thursday, July 11, 2019

Debug Django code in Docker ( Vscode)

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.~ Brian Kernighan

This is well known fact. Most of the time developers spend there time on debugging code rather than implementing new stuffs. however it's worth it. the reason is we can't write a piece of code that covers every aspect of the  requirement at the first time. modern day software development is not sole work.It's team work.hence most of the time we need to work with a code someone else has written. And last not the least ever thing in the world not reliable 100% then how could it possible that something work on top of silicon chips.

Vscode 

vscode is text editor, not an IDE, It's not cool or smart in python as pychram at the first appearance.However the best thing about VS code is It's highly customizable . Therefore if we can dedicate some time we can easily configured it to shopisticated IDE. I'm not going to spend time on how we can make VScode more beast for python development. In here you can find good article 


Django and Docker 


According to official website Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.
Truly it is. it's robust and feature rich framework and it has large community .

Then what are Dockers . A  container  (docker) is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.You can assume it as another OS on top of your OS, but that is really not the case.it's kind of bundle your application and it's dependccis and if it's works on your machine then it's works any other computer , cloud server or any other plat from which supports dockers. You can find more information from official website.

one downside of  Dockers is it's hard to debug ,because our application runs on docker container we have to connect that remote docker container in order to debug our code. Pycharm professional version has debugger for dockers however it's not wallet friendly.we need to buy licence. but we have alternative and it's 100% free. we can use VScode to connect docker and debug our application.



Prerequisites

As of this stage you need the following installed
  • Python 3
  • Docker

Installation of Prerequisite

Install dockers (if your already have dockers you may skip this step)
Here I'm not going to explain you have to make django docker container you can find comprehensive tutorial in here 

Project folder structure is like bellow



Docker file 

Docker compose file 
Then modify your mange.py file like bellow

We need to add three new lines as mentioned above to manage.py 
Next we need to start vscode and open our project 
next we need to add this configuration to vscode launch.json  file
we can add configuration to vscode by 
debug->open configuration 
launch.json looks  like this



Next we need build and start out docker container 
docker-compose build
then 
docker-compose run -p 3000:3000 -p 8000:8000 --rm web sh -c 'DJANGO_DEBUGGER=Tr python manage.py runserver --noreload --nothreading 0.0.0.0:8000'
Finally we need to start debug view in vscode ( CTRL+SHIFT+D )
select runserver as debug environment then hit play button



now you can add breakpoint and debug your code



That's it. Now you can enjoy your cool django project 
You can find full source code here  https://github.com/Nipuna-saga/django-docker-debugger-vscode