DEV Community

Daniel Cândido
Daniel Cândido

Posted on

How to debug a Python and Django application inside a Docker container

The rollercoaster of feelings while developing systems is something that anyone who has a minimum contact with this activity can strongly state. But if you have some relevant experience with many types of projects and applications, you know that the labor of building one from scratch (with all kinds of decisions and possibilities open) it's the dream situation.

The real nightmare it's when you need to maintain, to give support or to evolve an already existing and running system - sometimes with thousands or millions of users. And the last layer of the Inferno of Dante in software development is when you need to debug the code to fix something.

A big rollercoaster with normal people going up and lot of skeletons going down

I know that sometimes (common when you’re just beginning) you use the simplest, informal and renegade friend print and all its variants for other languages. No worries, our secret is safe here. But we also know that for the most painful and tricky bugs we need to use an efficient, powerful and really debugging tool.

I have been required to analyze some performance issues in a project recently, and the shallow investigation of function/class returning or time tracking with Grafana wasn’t enough to find the real problem in all the tangled of code.

Debugging

In Python we have PDB, a simple and practical library to debug our code. And to use it you only need to install it, with pip, for example:

pip install pdb 
Enter fullscreen mode Exit fullscreen mode

and then add one line in your code:

import pdb; pdb.set_trace()
Enter fullscreen mode Exit fullscreen mode

This will be your breakpoint, now you can run your application again and it will stop the execution right after the line that you just added. You are able to roll the execution down and deep in your code, line by line, function by function, and find the issue you crave to do.

With Docker

But in my current project I needed to do one more thing, as we use Django as a backend server running inside a Docker container. For those who have the same situation, you can follow this little next steps:

__

Now you can run your Docker image as well and connect with PDB debugger from the port 4444 that we opened by using a simple telnet command:

telnet 0.0.0.0 4444
Enter fullscreen mode Exit fullscreen mode

To cleverly sail through the sea of your code you will need to use some commands that PDB gives us. Let's see some useful here:


Practical example:


I hope this small content has been helpful to you. After facing a big performance issue investigation and with the learn reinforcement while write this text, I guarantee that

"use the right tool for the right job”

is something to never forget in our time as a Software Developer, which can move our career to a more effective level and give more joy in this rollercoaster ride.

Top comments (2)

Collapse
 
jennifer_williams_5678c6c profile image
Jennifer Williams

I’m delighted to have discovered this website! It’s incredibly informative and well-presented. Thank you for your insightful post and the effort you put into it. Please continue sharing more great content!
Feel free to visit my webpage as well. Check out the link here! best truck repair

Collapse
 
reynold82 profile image
Reynold82

Gran aporte para mí futuro. Recién empiezo a aprender programación con Python, y me parece que esto puede ser de gran ayuda cuando llegue a convertirme en un desarrollador. Ojalá llegue a eso 😃