1

I have a Python app (Flask, but this is likely incidental) which I have Docker-ized (using Docker Compose, which is again likely incidental). I have processes other than python/flask which need to run in the same container, and I user supervisord for this as it is convenient.

I set a pdb breakpoint pdb.set_trace() in my code and want to connect to my Flask app's TTY so I can interact with the (pdb) prompt. This usually works fine, but I'm not usually using supervisord. I can't get a reliable interactive (pdb) prompt - it looks like my terminal is not connected properly.

The usual steps to allow debugging (via docker attach) have failed me:

Telling docker-compose.yml to open the tty and connect stdin:

stdin_open: true
tty: true

Telling docker-compose.yml to set PYTHONUNBUFFERED:

PYTHONUNBUFFERED: 'true'

My supervisord block for the process is:

[supervisord]
nodaemon=true

[program:flask]
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
command=flask run

How can I work with pdb running under Docker and supervisord?

javabrett
  • 7,020
  • 4
  • 51
  • 73
  • None of the solutions here worked for me. [This one](https://stackoverflow.com/a/58540510/8285811) using `remote-pdb` did however. – Akaisteph7 Dec 19 '22 at 17:24

1 Answers1

2

To get this working, I had to abandon the usual docker attach approach on a container running in -it mode ... this did not work with any combination when running under supervisord.

What I had to do to debug was:

  1. Temporarily comment-out all four (4) of the std???_logfile??? lines in my supervisord.conf. Normally these are good for redirecting each processes' output back to docker logs, but they cause the loss of pdb output.
  2. Use supervisorctl to access the Flask application and interact with pdb.

The supervisorctl bit in-detail:

  1. Before or after your pdb.set_trace() breakpoint has been hit, run docker-compose exec <servicename> supervisorctl, or with plain Docker docker exec -it <containername> supervisorctl.
  2. At the supervisor> prompt, run fg <processname>, using the processname of your process running Flask/Python.
  3. You should get a (pdb) prompt when your breakpoint is hit. You may need to issue a (pdb) command such as l to get the terminal to issue a prompt.
  4. CTRL-C and exit when you are finished with supervisorctl.
javabrett
  • 7,020
  • 4
  • 51
  • 73
  • Sometimes it may be easier to use a remote debugger: https://github.com/Kozea/wdb https://pypi.org/project/rpdb/ – pawciobiel Oct 08 '21 at 11:16