How do you debug a Django application that is running in Docker?
Students of our advanced course on building REST API’s often ask: How can I use my editors integrated debugging tools when running Django in Docker?
It may surprise you, however personally I avoid using any integrating debugging tools where possible.
The reason for this is because I use Docker to isolate the dependencies of my project, and it’s often tedious to configure IDE’s (such as Python for VSCode or PyCharm) work in a containerized environment.
This is because these tools usually rely on the dependencies being installed directly on the local operating system.
Although editors such as VSCode and PyCharm have a Remote Debugging feature (for PyCharm you can find the steps here and VSCode here), I find that the tedious nature of these mean the juice is not worth the squeeze.
My preferred approach for debugging is to use the Python debug tools directly and step through it using the interactive debugger.
The steps for this are as follows:
- In the code, place a call to breakpoint() at the place you wish to start debugging
- Run the application using using docker-compose run (it won’t work with docker-compose up)
- When the code hits the breakpoint() line, the execution will pause and you will be able to use the Python debugger to step through or inspect variables.
If using docker-compose, you’ll need to run your code using the docker-compose run command, and provide the –service-ports flag if you need your ports to be mapped.
Leave a Reply
Want to join the discussion?Feel free to contribute!