DEV Community

Cover image for pdb | ipdb in docker-compose, enable interactive debugging
zahaar
zahaar

Posted on

pdb | ipdb in docker-compose, enable interactive debugging

pdb is a powerful debugging tool that is included as a Python standard library. It can seriously speed up, our debug process, while resolving certain bugs

for this

Problematic

Trying to initiate the pdb in your docker-compose container will result in the following error.

...
...
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/backend/app.py", line 25, in test_response
    return response
  File "/backend/app.py", line 25, in test_response
    return response
  File "/usr/local/lib/python3.9/bdb.py", line 88, in trace_dispatch
    return self.dispatch_line(frame)
  File "/usr/local/lib/python3.9/bdb.py", line 113, in dispatch_line
    if self.quitting: raise BdbQuit
bdb.BdbQuit

Enter fullscreen mode Exit fullscreen mode

that happens only if you try to run pdb in docker-compose and not via docker run ..., due to up not being an interactive command by design.

more on this topic: https://github.com/docker/compose/issues/4677

you can try in out yourself:

step 1. clone the repo setup repo with the minimal Flask app in docker-compose.
https://github.com/zahaar/docker-pdb-interactive-debugging/

step 2. cd docker-pdb-interactive-debugging
step 3. checkout on docker-error branch
git checkout docker-error
step 4. run docker compose up, or docker-compose up if you are using old docker CLI version
step 5. in a separate shell run curl command.

curl -XGET 'http://localhost:5000/api/v1.0/the_answer'

you would see the expected pdb.DdbQuit exception error:

...
...
  File "/usr/local/lib/python3.9/bdb.py", line 113, in dispatch_line
    if self.quitting: raise BdbQuit
bdb.BdbQuit
Enter fullscreen mode Exit fullscreen mode

Solution

pdb expects an open TTY, so let's add it to our compose file

...
...
    ports:
      - '5000:5000'
    stdin_open: true # für pdb
    tty: true # für pdb
Enter fullscreen mode Exit fullscreen mode

Let's try recreating the steps:

Step 1. git clone https://github.com/zahaar/docker-pdb-interactive-debugging/, but on a main branch this time
Step 2. docker compose up
Step 3. In a separate shell window attach local input to a running container, by running.

docker attach docker-interractive-debugging_api_1 // container name

Note we have this capability only after opening an TTY for our container

Step 4. In a separate shell window run.

curl -XGET 'http://localhost:5000/api/v1.0/the_answer'
Enter fullscreen mode Exit fullscreen mode

Step 5. now in the window where container output was attached you would see, pdb waiting for commands.

you can:

  • p true_response to print the value of the variable true_response
  • c to resume normal flow
  • to all other crazy things with pdb !!!

That's it. Easy.

Important Note: to stop attached window without stopping the main running container, you have to use,

escape sequence: CTRL + p -> CTRL + q

Tip:

checkout pdb commands cheatsheet

Top comments (1)