How to debug Django inside a docker — PDB
It took me hours to figure out how to add pdb to my Django project which resides in a Docker for software development. So I thought it would be great to make an article about it.
There can be mistakes in what I wrote and the things that I have conveyed, I am always ready to change or admit my mistake if found convincing.
Initialization
A sample project is created to demonstrate:
https://github.com/Joel-hanson/django-docker-pdb
Docker setup
We need to enable shell input for the docker service that runs Django. A tty
is essentially a text input-output environment aka shell. The stdin_open
is the interactive mode for a docker.
The stdin_open: true
stands for the flag -i
and the tty: true
stand for the flag -t
in docker
That’s it
The docker-compose setup for Django is over. Now we need is to add a import pdb; pdb.set_trace()
in the code and start debugging.
Attach to docker container
After setting up the docker-compose file, we need to run docker-compose up
to run the code.
The final step is to attach the docker container. To do this we need the docker container id of the Django service. we can get the docker id just but running docker ps
this will output all the running containers.
Thus we can attach the docker with the container id docker attach <container id>
That’s all folks
source code: https://github.com/Joel-hanson/django_docker_pdb
Please provide your feedback and suggestions. Follow me to get the latest updates.
Linkedin: linkedin.com/in/joel-hanson/
Portfolio: joel-hanson.github.io/
Github: github.com/Joel-hanson
Twitter: twitter.com/Joelhanson25