Using PDB and docker-compose
I’ve recently migrated my web application to run inside docker containers and use docker-compose
to spin up all the required dependencies in their own containers (Postgres and Redis, in my case). This application uses Python’s Flask framework and I use pdb
to debug issues.
While researching solutions, I first ran into this article by Vladyslav Krylasov that suggested adding some configuration to my docker-compose.yaml and using a module called remote-pdb
with a telnet client. This worked, but it was a little more cumbersome than I wanted.
My solution was to use docker attach
. First, I still needed to edit my docker-compose.yaml file to add the stdin_open
and tty
attributes to my application container:
application:
...
stdin_open: true
tty: true
Next, I needed to change the way I started my service. I edited my docker-compose up
line to run in the background with docker-compose up -d
and then ran docker attach application
to show all the container’s output in my console window. Now I get the app’s output (and it looks identical to how it did when I was running the Flask server locally) and can interact with it in pdb.
To test, I added a debugging line to a route:
@bp.route('/', methods=['GET'])
def index():
import pdb; pdb.set_trace()
return render_template(‘site/index.html’)
Then I pointed my browser to it and checked my console…
-> return render_template(‘site/index.html’)
(Pdb)
Beautiful!