DEV Community

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

Posted on

3 2

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

Getting started

start pdb from within a script: import pdb;pdb.set_trace()

start pdb from the commandline: python -m pdb <file.py>

Basics

h(elp) print available commands.
h(elp) command print help about command.
q(quit) quit debugger.

Examine

p(rint) expr print the value of expr.
pp expr pretty-print the value of expr.
w(here) print current position (including stack trace).
l(ist) list 11 lines of code around the current line.
l(ist) first, last list from first to last line number.
a(rgs) print the args of the current function.

Movement

<ENTER> repeat the last command.
n(ext) execute the current statement (step over).
s(tep) execute and step into function.
r(eturn) continue execution until the current function returns.
c(ontinue) continue execution until a breakpoint is encountered.
u(p) move one level up in the stack trace.

credits : https://github.com/nblock/pdb-cheatsheet

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay