Debug python containers pdb docker breakpoint

Debug python containers pdb docker breakpoint

Debugging python docker containers with remote-pdb, docker-compose

You want to debug and set breakpoints inside your docker container and/or you want to debug with docker-compose.

As the docs say: The debug host in your Docker container must be set to (localhost or will not work because Docker doesn’t map the port on the local interface).

Using Remove-pdb by ionel

pip install remote-pdb==2.1.0

python3 -m venv venv
. venv/bin/activate

Build example Dockerfile


FROM python:3.9.6-alpine3.14

WORKDIR /usr/src/app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD [ "python", "./" ]

Build it:

docker build -t example .

Run the docker container

docker run -p 4444:4444 example 

Where did 4444 come from? It comes from the , we
tell remote-pdb which port to listen on- you can choose any port.

Start debugging!

  1. Ensure you have imported remote-pdb
from remote_pdb import RemotePdb
RemotePdb('', 4444).set_trace()

Note: You only need to do this once.

  1. Put a breakpoint in your code (e.g. breakpoint())
  2. telnet 4444

Example docker-compose file

version: "3.9"
    stdin_open: true
    tty: true
    build: django
      - "8001:8000"
      - "4444:4444" # This is the debugger port
Collect recurring payments with Subscribie - Try Now