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.
A sample project is created to demonstrate:
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.
stdin_open: true stands for the flag
-i and the
tty: true stand for the flag
-t in docker
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 docker id
Thus reaching the breakpoint will be as follows