Debugging: Using PDB in Dockerized Environment

Debugging: Using PDB in Dockerized Environment

·

0 min read

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:

Screenshot from 2020-01-13 17-14-47.png

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>

Screenshot from 2020-01-13 17-29-21.png

Here you go! Now, you are in your PDB session.

Screenshot from 2020-01-13 17-31-00.png