


The rollercoaster of emotions experienced while developing systems is something that anyone with even minimal exposure to this can strongly state. However, for those with significant experience across various projects and applications, building a system from scratch is often considered the dream situation.
The real nightmare arises when you need to maintain, support, or evolve an existing and running system - sometimes with thousands or millions of users. And the deepest layer of The Inferno of Dante in software development is when you have to debug the code to fix an issue.
I know that sometimes, especially when you’re just starting out, you rely on the simplest and most informal print() and its equivalents in other languages. No worries, our secret is safe here. However, we also know that for the most painful and tricky bugs, we need to use an efficient, powerful, and comprehensive debugging tool.
I have been required to analyze some performance issues in my work. A shallow investigation of function/class returns or time tracking with Grafana wasn’t enough to find the real problem in all the tangled code. So... let's debug!
Debugging
In Python, we have PDB, a straightforward library for debugging our code. To use it, you simply need to install it using pip. For example:
pip install pdb
Then add one line in the code you want to debug:
import pdb; pdb.set_trace()
This will be your breakpoint. Now, you can run your application again, and it will pause execution right after the line you just added. You can then step through your code, line by line and function by function, to find the issue you need to address.
With Docker
The setup above should suffice for a simple Python project with Django, Flask, or FastAPI. However, if you've reached this point and are running your application inside a Docker container, facing issues, I've been on the same road. Stay calm and simply follow these next few steps:
1 - Instead of the usual PDB, you should install the remote debugger. Run it inside your Docker container (or include the library in your dependencies manager):
pip install remote-pdb
2 - In your docker-compose.yml file, add a new port to expose the PDB entrypoint:
... services: your-app: ... ports: - 8000:8000 # already existing port of your application - 4444:4444 # NEW LINE TO EXPOSE PDB PORT ... ...
3 - Still in your docker-compose.yml file, add these two lines, to allow sending commands from outside Docker to your running container:
... services: your-app: ... ports: - 8000:8000 - 4444:4444 stdin_open: true # THIS tty: true # AND THIS ... ...
4 - Finally, change the breakpoint line in your code to this one:
__import__("remote_pdb").set_trace(host='0.0.0.0', port=4444)
Now you can run your Docker image as well and connect with PDB debugger from port 4444, which we opened, using a simple telnet command in your favorite terminal:
telnet 0.0.0.0 4444
PDB Commands
To cleverly sail through the sea of your code, you'll need to use the helpful commands provided by PDB. Let's see some of them here:
I normally just use the commands step (s) and next (n). The difference between both is that step execute each line, even those inside a function that is called on the current one. And next only execute the lines inside the current function, waiting for the return of called functions.
Other two useful commands are return (r), that execute all the function until it returns, and continue (c), that execute everything until the next breakpoint.
Conclusion
I hope this content has been helpful to you. Through investigating a significant performance issue and reinforcing my learning while writing this text, I can guarantee that
"using the right tool for the right job”
is something we should never forget as software developers. It can elevate our careers to a more effective level and bring more joy to this rollercoaster ride.
The above is the detailed content of How to debug a Python and Django application inside a Docker container. For more information, please follow other related articles on the PHP Chinese website!

The article discusses Python's new "match" statement introduced in version 3.10, which serves as an equivalent to switch statements in other languages. It enhances code readability and offers performance benefits over traditional if-elif-el

Exception Groups in Python 3.11 allow handling multiple exceptions simultaneously, improving error management in concurrent scenarios and complex operations.

Function annotations in Python add metadata to functions for type checking, documentation, and IDE support. They enhance code readability, maintenance, and are crucial in API development, data science, and library creation.

The article discusses unit tests in Python, their benefits, and how to write them effectively. It highlights tools like unittest and pytest for testing.

Article discusses access specifiers in Python, which use naming conventions to indicate visibility of class members, rather than strict enforcement.

Article discusses Python's \_\_init\_\_() method and self's role in initializing object attributes. Other class methods and inheritance's impact on \_\_init\_\_() are also covered.

The article discusses the differences between @classmethod, @staticmethod, and instance methods in Python, detailing their properties, use cases, and benefits. It explains how to choose the right method type based on the required functionality and da

InPython,youappendelementstoalistusingtheappend()method.1)Useappend()forsingleelements:my_list.append(4).2)Useextend()or =formultipleelements:my_list.extend(another_list)ormy_list =[4,5,6].3)Useinsert()forspecificpositions:my_list.insert(1,5).Beaware


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
