Melvin Koh

Software Engineer. Pythonista, JavaScripter. Find me on Twitter @melvinkcx2 😁

Debugging: Using PDB in Dockerized Environment

Docker, along with Docker Compose are the most used tools under the DevOps category, according to The State of Developer Ecosystem 2019 survey by JetBrains. Chances are if you're using Docker/Docker Compose for deployment, you'll most likely be using them for local development as well. If you only use Docker for deployment but a virtual environment for local development, you may want to look into using Docker for development to reduce the parity between dev/prod environments, as suggested in the Twelve-Factor Methodology.
In this article, you'll be learning how to use PDB (Python DeBugger) in a Dockerized Python environment. If you are reading this, I believe you already have your Docker set up and are perhaps trying to solve the
bdb.BdbQuit
error:
For the sake of simplicity, I'll be using a toy app I created throughout this tutorial. You can find it here.
In order to use
pdb
, you need to run your containers in interactive mode. To do so, ensure your Docker container accepts
stdin
and runs in
tty
mode by adding these two lines in your Docker service in your
docker-compose.yml
(or your docker-compose configuration)
version: '3'

services:
  web:
    build: .
    volumes:
      - .:/app
    stdin_open: true   # Add this line into your service
    tty: true   # Add this line into your service
After those lines are added, start your Docker container as usual. In my case, I run:
docker-compose up -d
Once your Docker container is running, attach your terminal standard streams to your running container:
docker attach <your_container_id>
Here you go! Now, you are in your PDB session.

Tags

More by Melvin Koh

Python
Docker
Slack
Integrate Google Calendar
Git
Memory Improvement
Python
Nodejs
Firebase
Postgres
Productivity
Programming
Python
Microservices
Facebook
Javascript
Django
Django
Firestore
Javascript
Vuejs
Jwt
Topics of interest